![](/img/trans.png)
[英]How to Split the_content() and store into array in wordpress where <!--nextpage-->
[英]regular expressions : Splitting by <!--nextpage--> in wordpress content
我很麻煩,因為我不了解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版本7.1.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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.