Hello Community,
I was wanting to make a bbcode system using regex but the one I'm having a problem with is the list (<ol> and <ul>) but for some reason it isn't working this is the code I'm using:

<?php
    function convertBBCode($input) {
      $bbCode = array(
          "/\[b\](.*?)\[\/b\]/",
          "/\[u\](.*?)\[\/u\]/",
          "/\[i\](.*?)\[\/i\]/",
          "/\[s\](.*?)\[\/s\]/",
          "/\[url\](.*?)\[\/url\]/",
          "/\[url\=(.*?)\](.*?)\[\/url\]/",
          "/\[img\](.*?)\[\/img\]/",
          "/\[ul\](.*?)\[\/ul\]/",
          "/\[ol\](.*?)\[\/ol\]/",
          "/\[li\](.*?)\[\/li\]/",
          "/\[code\](.*?)\[\/code\]/"
      );

      $htmlCode = array(
          "<b>\\1</b>",
          "<u>\\1</u>",
          "<i>\\1</i>",
          "<s>\\1</s>",
          "<a href=\"\\1\" target=\"_blank\">\\1</a>",
          "<a href=\"\\2\" target=\"_blank\">\\1</a>",
          "<img src=\"\\1\">",
          "<ul>\\1</ul>",
          "<ol>\\1</ol>",
          "<li>\\1</li>",
          "<code>\\1</code>"
      );

      return preg_replace($bbCode, $htmlCode, $input);
    }
    echo convertBBCode($_POST['inputText']);
?>

<form action="index.php" method="POST">
    <textarea name="inputText">
[ol]
[li]Hello[/li]
[/ol]
    </textarea>
    <input type="submit"/>
</form>

Try the code out and you'll see what's happening, which is just inserting the [li]Hello[/li] and not the [ol] and [/ol] I've looked online for any examples on why it's happening but I'm thinking it could have something with the regex line thinking it isn't reading the newline (\n).

Recommended Answers

All 3 Replies

Thank you but not a big deal but is there a way to make it so it doesn't insert newlines because I'm getting what I wanted but between each list item there is a linebreak. So is there a way to remove the <br>?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.