0

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);
0

2 Answers 2

2
$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);
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is missing its explanation.
1

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]\n[*]Камиль [/*]\n[*]Хисматуллин [/*]\n[*]живет в Урюпинске [/*]\n[/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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.