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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:25:38
Original
1307 people have browsed it

形如:

<code>$str='<p>hhhhhhhhhhhhh</p>'

$str='<h1>jjjjjjjjjjjjjjjj</h1>'

</code>
Copy after login
Copy after login

想要得到p或者h1之间的内容,怎么写?

回复内容:

形如:

<code>$str='<p>hhhhhhhhhhhhh</p>'

$str='<h1>jjjjjjjjjjjjjjjj</h1>'

</code>
Copy after login
Copy after login

想要得到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>
Copy after login

运行结果:

<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>
Copy after login

$reg='#(.+?)#s'

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template