我遇到麻烦,因为我不知道php的正则表达式。
原始字符串在下面。
$the_content = "
<h2>1page</h2><p>...</p>
<!--nextpage-->
<h2>2page</h2><p>...</p>
<!--nextpage-->
<h2>3page</h2><p>...</p>
<!--nextpage-->
<h2>4page</h2><p>...</p>
<!--nextpage-->
<h2>5page</h2><p>...</p>
";
我想要的返回数组如下。
$matches = array(
0 => '<h2>1page</h2><p>...</p><!--nextpage-->',
1 => '<h2>2page</h2><p>...</p><!--nextpage-->',
2 => '<h2>3page</h2><p>...</p><!--nextpage-->',
3 => '<h2>4page</h2><p>...</p><!--nextpage-->',
4 => '<h2>5page</h2><p>...</p>',
);
//or
$matches = array(
0 => '<h2>1page</h2><p>...</p>',
1 => '<!--nextpage-->',
2 => '<h2>2page</h2><p>...</p>',
3 => '<!--nextpage-->',
4 => '<h2>3page</h2><p>...</p>',
5 => '<!--nextpage-->',
6 => '<h2>4page</h2><p>...</p>',
7 => '<!--nextpage-->',
8 => '<h2>5page</h2><p>...</p>',
);
php ver 7.1.0
答案 0 :(得分:0)
您可以在HTML评论(?<=<!--nextpage-->)
代码示例:
<?php
$sourcestring='
<h2>1page</h2><p>...</p>
<!--nextpage-->
<h2>2page</h2><p>...</p>
<!--nextpage-->
<h2>3page</h2><p>...</p>
<!--nextpage-->
<h2>4page</h2><p>...</p>
<!--nextpage-->
<h2>5page</h2><p>...</p>
';
$matches=preg_split('/(?<=<!--nextpage-->)',$sourcestring);
echo print_r($matches,true);