2017-08-22 13 views
1

[nextpage]bbcodeの後の任意の文字に一致させたいが、以下のbbcodeは改行するときに続くテキストと一致しない。Regexで改行するすべての文字を一致させる

$string="[nextpage] This is how i decided to make a living with my laptop. 
This doesn't prevent me from doing some chores, 

I get many people who visits me on a daily basis. 

[nextpage] This is the second method which i think should be considered before taking any steps. 

That way does not stop your from excelling. I rest my case."; 

$pattern="/\[nextpage\]([^\r\n]*)(\n|\r\n?|$)/is"; 
preg_match_all($pattern,$string,$matches); 
$totalpages=count($matches[0]); 
$string = preg_replace_callback("$pattern", function ($submatch) use($totalpages) { 
$textonthispage=$submatch[1]; 
return "<li> $textonthispage"; 
}, $string); 
echo $string; 

これは、最初の行のテキストのみを返します。

<li> This is how i decided to make a living with my laptop. 

<li> This is the second method which i think should be considered before taking any steps. 

予想される結果です。

<li> This is how i decided to make a living with my laptop. 
This doesn't prevent me from doing some chores, 

I get many people who visits me on a daily basis. 

<li> This is the second method which i think should be considered before taking any steps. 

That way does not stop your from excelling. I rest my case. 

答えて

0

あなたは、この正規表現を使用して検索することがあります。

\[nextpage]\h*(?s)(.+?)(?=\[nextpage]|\z) 

によって置き換え:

<li>$1 

RegEx Demo

PHPコード:

$re = '/\[nextpage]\h*(?s)(.+?)(?=\[nextpage]|\z)/'; 
$result = preg_replace($re, '<li>$1', $str); 

Code Demo

正規表現の分裂:

\[nextpage]   # match literal text "[nextpage]" 
\h*     # match 0+ horizontal whitespaces 
(?s)(.+?)   # match 1+ any characters including newlines 
(?=\[nextpage]|\z) # lookahead to assert that we have another "[nextpage]" or end of text 
0

あなたが正規表現はならない固定文字列を持っている場合。正規表現は高価であり、簡単なstr_replaceは、同様のトリックを行います。

$result = str_replace("[nextpage]", "<li>", $str); 

あなたが適切なHTMLとしてそれをしたい場合は、あなたにも終了タグが必要になります。

$result = str_replace("[nextpage]", "</li><li>", $string); 
$result = substr($result, 5, strlen($result)).'</li>'; // remove the start </li> 

echo $result; 
関連する問題