2017-08-22 3 views
1

Ich möchte jedes Zeichen nachBBCode übereinstimmen, aber der unten BBCode stimmt nicht überein Texte, die folgen, wenn ich eine Bruchlinie mache.PHP Match alle Zeichen in der neuen Zeile mit 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; 

Dies gibt nur die Texte in der ersten Zeile zurück.

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

Erwartetes Ergebnis;

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

Antwort

0

können Sie suchen diese regex:

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

ersetzen durch:

<li>$1 

RegEx Demo

PHP-Code:

Code Demo

RegEx Breakup:

\[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

Wenn Sie eine feste Zeichenfolge Sie sollten nicht regex. Regex ist teuer, eine einfache str_replace funktioniert den Trick auch:

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

Wenn Sie es als richtiger HTML, Sie auch einen engen andneedtag:

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

echo $result;