Home >Backend Development >PHP Tutorial >php 怎么写个正则取出 [attach]1[/attach]里的东西

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:47:071271browse

php 如何写个正则取出 [attach]1[/attach]里的东西
内容[attach]1[/attach]大帅府[attach]2[/attach] 你的那份

怎么得到[attach][/attach]之间的内容1和2.的数组
------解决方案--------------------

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


没测试!
------解决方案--------------------
/[attach](.*?)[\/attach]/i
 刚忘写转义了
------解决方案--------------------
<br />preg_match_all('/\[attach\](.*?)\[\/attach\]/i', '[attach]1[/attach]大帅府[attach]2[/attach] 你的那份', $out);<br />print_r($out);<br />


逼我给你测试!


------解决方案--------------------
上面截图的数组都出来了 剩下的自己会搞了吧
------解决方案--------------------
$str = "内容[attach]1[/attach]大帅府[attach]2[/attach] 你的那份";<br />preg_match_all('/\[attach](.*?)\[\/attach]/i',$str,$ret);<br />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