Home  >  Article  >  Backend Development  >  php 如何写个正则取出 [attach]1[/attach]里的东西

php 如何写个正则取出 [attach]1[/attach]里的东西

WBOY
WBOYOriginal
2016-06-23 14:02:011163browse

内容[attach]1[/attach]大帅府[attach]2[/attach] 你的那份

怎么得到[attach][/attach]之间的内容1和2.的数组


回复讨论(解决方案)

preg_match_all('/[attach](.*?)[/attach]/i', '[attach]1[/attach]大帅府[attach]2[/attach] 你的那份', $out);print_r($out);


没测试!

/[attach](.*?)[\/attach]/i
 刚忘写转义了

貌似不对啊大哥,哈有怎么输出结果啊

想输出为
1
2

preg_match_all('/\[attach\](.*?)\[\/attach\]/i', '[attach]1[/attach]大帅府[attach]2[/attach] 你的那份', $out);print_r($out);


逼我给你测试!

上面截图的数组都出来了 剩下的自己会搞了吧

$str = "内容[attach]1[/attach]大帅府[attach]2[/attach] 你的那份";preg_match_all('/\[attach](.*?)\[\/attach]/i',$str,$ret);print_r($ret[1]);

输出:
Array ( [0] => 1 [1] => 2 ) 

可以参考:http://clqwtt.blog.163.com/blog/static/22351105520139155735792/

也可以用php simple_html_dom实现,参考:http://clqwtt.blog.163.com/blog/static/223511055201391694845458/

preg_match_all('/\[attach]([^\[]*)\[\/attach]/isU',$str,$ret);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn