View Full Version : [PHP] Requesting a piece of code
Gecko
June 23rd, 2007, 11:35 PM
I need a piece of code that will check a long string for [ b ][ /b ] tags (BBcode) and replace them with <strong></strong>
BUT! And here's the catch, it also has to check what is inside those tags (between [ b ] and [ /b ]), and if it finds any "</p><p>" it has to replace them with </strong></p><p><strong>.
I've used RegEx to find the BBcode tags, but checking what's between them is almost impossible for me to figure out. So I'm asking for help. Any method you can possibly think of to do this, please post.
Actually, what I would REALLY need is a check to see if there are any unclosed tags inside the BBcode tags to prevent nestling issues. But that sounds to complex to ask for. However if anyone would be able to manage it I would be ever so grateful. I could even offer to return the favor by offer my webdesign expertise. I can make you a layout since I'm pretty good at that stuff.
Anyway, thanks in advance.
Geometric-sama
June 24th, 2007, 02:07 PM
Easiest way is to scan in the text iteratively, checking for your <p> and </p> and using flags to mark them, but that's going to be slow. Depends what kind of blocks of text you're processing. Should be fine for small amounts.
Gecko
June 24th, 2007, 10:00 PM
Uhh, yeah, thanks for the effort, but that doesn't really help me.
Faltzer
June 25th, 2007, 02:32 AM
IIRC, does streplace work for that?
Geometric-sama
June 25th, 2007, 07:50 AM
Uhh, yeah, thanks for the effort, but that doesn't really help me.
What details do you need?
Gecko
June 25th, 2007, 03:07 PM
I think I made it pretty clean. I just wanna make sure that the script checks the content between the tags and find unclosed tags. In other words, I wanna prevent nestling issues in user posts.
Geometric-sama
June 26th, 2007, 10:38 AM
So... I gave you instructions on the only way I can think of to do it. How doesn't it help you~?
Gecko
June 26th, 2007, 12:53 PM
Easiest way is to scan in the text iteratively, checking for your <p> and </p> and using flags to mark them, but that's going to be slow.
Those aren't instructions, they suggestions. Suggestions I have already thought of, but have been unable to execute.
Faltzer
June 26th, 2007, 02:10 PM
http://www.phpit.net/article/create-bbcode-php/
Tyler
June 26th, 2007, 07:20 PM
Why not look at Forum's BBcode layouts in the coding
Gecko
June 26th, 2007, 08:46 PM
Because forums usually have <br /> (or just <br>) tags for all lines breaks. I have made my site a little different by only allowing one line break. If there is more than 1 continuous line break it is automatically turned into </p><p>. So it's either one line break or a new paragraph, no other options.
So the reason I need it this particular way is because users might post a and then do several line breaks before they close it. It on other words, there would be one or more </p><p> between the opening and closing tag. And as many of you probably know, that's not valid... So, I need a method of checking for and replacing them with <strong></strong> (which I have already achieved on my own with RegEx. Very simple) [b]AND it also has to be able to read the text BETWEEN the tags so that I can check for unclosed or unopened tags
------
Thanks Faltzer, but that also didn't have what I required.
Faltzer
June 26th, 2007, 09:29 PM
Gecko, I'm sure you missed this part:
http://www.phpit.net/article/create-bbcode-php/2/
That's for nesting BBCode ([ b ][ p ] [/ p] [/ b ])
Gecko
June 26th, 2007, 10:54 PM
I didn't miss it. However, that will not help me. That script only checks if any tag is unclosed, and if it is, it adds a closing tag at the end of the string, which is not a good method if the string contains </p><p> as it wouldn't nestle within them. That tutorial deals with forum BBcodes, and as I mention most forums use <br /> separation and not the <p> tag method I described in my last post.
Faltzer
June 27th, 2007, 12:22 AM
Hm, your case would be best left to ask PHPFreaks, instead.
Tyler
June 29th, 2007, 05:56 PM
I think i have what you want.
echo "</p><p>" . bbcode(n12br(htmlspecialchars($body))) . "</p>";
I hope that helps for what you want, not sure if thats it but thats what i've come up with.
Gecko
June 29th, 2007, 10:58 PM
Uhh, that was pretty pointless. Have you even read what I've said? It's not the BBcodes themselves. That's easy. My issue is with nestling them. Just read what I wrote in Post number 11 (http://www.pkmncommunity.com/showpost.php?p=2560243&postcount=11)