I have bbcodes at page:
[list=1]
[*]Камиль [/*]
[*]Хисматуллин [/*]
[*]живет в настоящий [/*]
[/list]
How I can replace these bbcodes to HTML tags:
<ul>
<li></li>
<li></li>
<li></li>
</ul>
I tried regular expression:
$advanced_bbcode = array(
'#\[list=([0-9]?)](.+)\[/list]#Usi',
'#\[*](.+)\[/*]#Usi'
);
$advanced_html = array(
'<ol>$1</ol>',
'<li>$1</li>'
);
$text = preg_replace($advanced_bbcode, $advanced_html,$text);
You need to adjust the regex a bit (add the Singleline
inline (?s)
option that can be combined with case-insensitive (?i)
option), the rest is neat. Only I do not know if you need <ol>
or <ul>
(you can adjust that part yourself). Here is my solution (tested on TutorialsPoint):
<?php
$str = "[list=1]
[*]Камиль [/*]
[*]Хисматуллин [/*]
[*]живет в Урюпинске [/*]
[/list]";
$advanced_bbcode = array(
'/(?si)\\[list=\\d+\\](.*?)\\[\\/list\\]/',
'/(?si)\\[\\*\\](.*?)\\[\\/\\*\\]/'
);
$advanced_html = array(
'<ol>$1</ol>',
'<li>$1</li>'
);
$text = preg_replace($advanced_bbcode, $advanced_html, $str);
echo $text;
?>
Output:
<ol>
<li>Камиль </li>
<li>Хисматуллин </li>
<li>живет в Урюпинске </li>
</ol>
$advanced_bbcode = array(
'#\[list=[0-9]+\](.+)\[\/list\]#i',
'#\[\*\](.+)\[\/\*\]#i'
);
$advanced_html = array(
'<ol>$1</ol>',
'<li>$1</li>'
);
$text = preg_replace($advanced_bbcode, $advanced_html, $text);