max
max

Reputation: 97

how to replace bbcode li?

I need to create a function to replace

[li]text[/li]

into

 <li>text</li>

This is the regex Im testing but nothing

 $string="[li]A[/li] [li]B[/li] [li]C[/li] [li]D[/li]";

 $pattern = "/[li](.*?)[\/li]/s";
$string = preg_replace($pattern, "$1", $string);

 echo $string; //but returns: []A[/] []B[/] []C[/] []D[/]

but nothing, what Im doing wrong?

Upvotes: 1

Views: 86

Answers (2)

Majid Shafaei
Majid Shafaei

Reputation: 59

$string="[li]A[/li] [li]B[/li] [li]C[/li] [li]D[/li]";
$string = preg_replace('/\[(.*?)\](.*?)\[\/(.*?)\]/', '<$1>$2</$3>', $string);
echo $string;

Result:

<li>A</li> <li>B</li> <li>C</li> <li>D</li>

Upvotes: 0

Antonio Abrantes
Antonio Abrantes

Reputation: 591

you should insert the < li > in the replace command

$string="[li]A[/li] [li]B[/li] [li]C[/li] [li]D[/li]";
$pattern = "/\[li\](.*?)\[\/li\]/s";
$string = preg_replace($pattern, "<li>$1</li>", $string);
echo "<pre>";
echo $string;
echo "</pre>";

Upvotes: 1

Related Questions