ホームページ >バックエンド開発 >PHPチュートリアル >如何用正则解析这样的字符串?
形如:
<code>$str='<p>hhhhhhhhhhhhh</p>' $str='<h1>jjjjjjjjjjjjjjjj</h1>' </code>
想要得到p或者h1之间的内容,怎么写?
形如:
<code>$str='<p>hhhhhhhhhhhhh</p>' $str='<h1>jjjjjjjjjjjjjjjj</h1>' </code>
想要得到p或者h1之间的内容,怎么写?
<code><?php $reg = '/<([a-z0-9]+)>([\s\S]+?)/'; $str='<p>hhhhhhhhhhhhh</p>'; preg_match($reg, $str, $result); var_dump($result); $str='<h1>jjjjjjjjjjjjjjjj</h1>'; preg_match($reg, $str, $result); var_dump($result); </code>
运行结果:
<code>array(3) { [0]=> string(20) "<p>hhhhhhhhhhhhh</p>" [1]=> string(1) "p" [2]=> string(13) "hhhhhhhhhhhhh" } array(3) { [0]=> string(25) "<h1>jjjjjjjjjjjjjjjj</h1>" [1]=> string(2) "h1" [2]=> string(16) "jjjjjjjjjjjjjjjj" } </code>
$reg='#(.+?)#s'