Home >Backend Development >PHP Tutorial >如何用正则解析这样的字符串?

如何用正则解析这样的字符串?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:25:381320browse

形如:

<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'

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