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