I have recently coded out a BBCode system for parts of my website using
preg_replace and
function, and all of it works when it comes to creating tags which require input between them. However, since I created this BBCode file and included it in my page, it seems to override my former
str_replace property I used to automatically convert line breaks to
<br /> tags and paragraphs to
</p><p> tags in the appropriate spots. I have removed this part of code now, since it doesn't function at all with the BBCode include.
So, I have inserted the similar code for paragraph conversion into my BBCode file and this is what I have:
<?php
function bbcode($input){
$input = strip_tags($input);
$input = htmlentities($input);
$search = array(
'/\r\n\r\n/is',
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url=(.*?)\](.*?)\[\/url\]/is',
'/\[urlnw=(.*?)\](.*?)\[\/urlnw\]/is',
);
$replace = array(
'</p>
<p>',
'<span style="font-weight:bold">$1</span>',
'<span style="font-style:italic">$1</span>',
'<span style="text-decoration:underline">$1</span>',
'<a href="$1">$2</a>',
'<a href="$1" target="_blank">$2</a>',
);
}
?>
The following lines focus on the line to paragraph conversion:
'/\r\n\r\n/is',
'</p>
<p>',
I use the return between the paragraph tags to make the HTML markup tidy when viewing the source of the page.
That's all fine - works perfectly if I have a blank line in between text and the paragraphs are automatically put into place. However, when I try adding another code to represent single line breaks, the BBCode function is forced to place the
<br /> tag in the right place, however the paragraph code kicks in and treats this tag as another blank line in addition to the new line I started using the 'return' key.
I can sort of understand why it's doing this, but I'm wondering if there is a solution and what it could be. My hunch is an
if statement to tell the paragraph code not to execute when only one line break is present. I'm also wondering is
str_replace could be used in the BBCode file as that was working perfectly before and treated new manually typed lines as a single
<br /> tag and two as the paragraph tag.
If anybody could help with this I'd appreciate it a lot, thanks. :)