Re: [PHP] Problem with RegEx for BBCode
On 27 November 2010 13:57, Asmann, Roland roland.asm...@adesso.at wrote: Hi all, I am playing around with PHP and BBCodes and have found some regex's that should transform my BBCode into correct HTML when rendering. However, I have found that if the BBCode is not correct (eg missing closing tag), the regex completely eats my input and my page is empty! The regex I'm using is: /\[i\]((\s|.)+?)\[\/i\]/ And with an input like: This is [i]italic. I get nothing back. What I would like is that when no closing tag is found, the opening tag should just be shown as-is. Anybody have any idea how I can do this? Thanks! -- Roland Asmann Senior Software Engineer adesso Austria GmbH Floridotower 26. Stock T +43 1 2198790-27 Floridsdorfer Hauptstr. 1 F +43 1 2198790-927 A-1210 Wien M +43 664 88657566 E roland.asm...@adesso.at W www.adesso.at - business. people. technology. - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php I think this is a common issue with regex. Trying to use Regex as a parser is no good. At best, regex can be used as a tokenizer. It has limited ability to handle the nesting. The input you gave has no closing [/i], so the regex can't match the string, so nothing is returned. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with RegEx for BBCode
On 27-11-10 18:44, Asmann, Roland wrote: On 27-11-10 17:24, Daniel P. Brown wrote: On Sat, Nov 27, 2010 at 08:57, Asmann, Roland roland.asm...@adesso.at wrote: Hi all, I am playing around with PHP and BBCodes and have found some regex's that should transform my BBCode into correct HTML when rendering. However, I have found that if the BBCode is not correct (eg missing closing tag), the regex completely eats my input and my page is empty! [snip!] What I would like is that when no closing tag is found, the opening tag should just be shown as-is. Anybody have any idea how I can do this? Have you checked into the built-in BBCode library? http://php.net/bbcode I'm afraid that is not an option, I don't have that package installed and am not allowed to install anything on the server either... Besides, what I don't really understand is WHY does this happen? Any other language that has RegEx doesn't match a thing in my example and just returns the original input. Why is PHP different in this regard? -- Roland Asmann Senior Software Engineer adesso Austria GmbH Floridotower 26. Stock T +43 1 2198790-27 Floridsdorfer Hauptstr. 1 F +43 1 2198790-927 A-1210 Wien M +43 664 88657566 E roland.asm...@adesso.at W www.adesso.at - business. people. technology. - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with RegEx for BBCode
On Sat, Nov 27, 2010 at 12:50, Asmann, Roland roland.asm...@adesso.at wrote: Besides, what I don't really understand is WHY does this happen? Any other language that has RegEx doesn't match a thing in my example and just returns the original input. Why is PHP different in this regard? All we've seen is your regexp, not your code - not even which regexp library you're using. So that's a pretty ambiguous question, really. -- /Daniel P. Brown Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting (866-) 725-4321 http://www.parasane.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with RegEx for BBCode
Please be sure to hit Reply-All so that the list is CC'd on each response as well. On Sat, Nov 27, 2010 at 13:55, Asmann, Roland roland.asm...@adesso.at wrote: I'm currently running this on a default XAMPP installation on my PC, because I was told something similar is running on the server. I thought this had a default RegEx lib, or does it have something extra for that? No, I mistyped, it's not your fault. I meant: which function are you using to execute the regexp? As for the code, here's the part that I think *should* work, but doesn't do the trick for me: $s = preg_replace(/\[i\]((\s|.)+?)\[\/i\]/, i\\1/i, $s); This is exactly what I meant when I erroneously said, library. -- /Daniel P. Brown Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting (866-) 725-4321 http://www.parasane.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php