killah Posted February 26, 2009 Share Posted February 26, 2009 As the title say's. This bit of code is giving some error. <?php class bbcode_engine { function bbcode_parser($code) { $string = ''; $string .= preg_replace('[b]/(\.*?/)\[/b]','<strong>$1</strong>',$code); return $string; } } $bbcode = new bbcode_engine(); echo $bbcode->bbcode_parser('[b]Testing[/b]'); ?> This is the error i am getting Warning: preg_replace() [function.preg-replace]: Unknown modifier '/' in /home/deadlyki/public_html/test.php on line 6 Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/ Share on other sites More sharing options...
killah Posted February 26, 2009 Author Share Posted February 26, 2009 I have figured a solution. $search = array ( '/(\[[bb]\])(.+)(\[\/[bb]\])/', '/(\[[ii]\])(.+)(\[\/[ii]\])/', '~\[size=(.+)\](.+)\[\/size\]~is', '/\n/' ); $replace = array ( '<strong>\\2</strong>', '<em>\\2</em>', '<span style="font-size: \\1;">\\2</span>', '<br />', ); Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/#findComment-771535 Share on other sites More sharing options...
.josh Posted February 26, 2009 Share Posted February 26, 2009 you are missing delimiters and also you aren't properly escaping the brackets. $string .= preg_replace('~\[b\](.*?)\[/b\]~','<strong>$1</strong>',$code); Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/#findComment-771537 Share on other sites More sharing options...
killah Posted February 26, 2009 Author Share Posted February 26, 2009 I had figured it out. But now i have ran into another problem. My code is: <?php class bbcode_engine { function bbcode_parser($code) { $search = array ( '~\[b\](.+)\[\/b\]~is', '~\[i\](.+)\[\/i\]~is', '~\[size=(.+)\](.+)\[\/size\]~is', '/\n/', '~\ [center](.+)\[\/center\]~is', '~\[left](.+)\[\/left\]~is', '~\[right](.+)\[\/right\]~is', ); $replace = array ( '<strong>\\1</strong>', '<em>\\1</em>', '<span style="font-size: \\1;">\\2</span>', '<br />', '<div align="center" style="padding: 0;">\\1</div>', '<div align="left" style="padding: 0;">\\1</div>', '<div align="right" style="padding: 0;">\\1</div>' ); return preg_replace($search, $replace, $code); } } $bbcode = new bbcode_engine(); echo $bbcode->bbcode_parser('We try [b]bold[/b] Then we try [i]italic[/i] now we try size [size=24]24[/size] [center][b]Center[/b][/center] [left]Left[/left] [right]Right[/right] '); ?> But it output's it like this: We try bold[/ b] Then we try italic now we try size 24 [ b]Center Left Right You can see it at http://www.deadlykillah.com/test.php. Any fix for this? Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/#findComment-771548 Share on other sites More sharing options...
killah Posted February 26, 2009 Author Share Posted February 26, 2009 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/#findComment-771799 Share on other sites More sharing options...
killah Posted February 27, 2009 Author Share Posted February 27, 2009 BUMP* Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/#findComment-772826 Share on other sites More sharing options...
.josh Posted February 28, 2009 Share Posted February 28, 2009 it's because your (.+) is greedy. You have 2 bold tags in your string so the (.+) in between your first b tags is matching all the way to the last closing b tag. Make it non-greedy by doing this (.+?) for all of them. But also, you're going to run into problems later on if someone does this: [b][/b] with any of your tags, not just the b tags. I'm pointing out to you the lack of stuff between them. Your patterns will cause problems because the + expects at least one character. So use * instead of +. * is 0 or more chars. So overall, change all your (.+) to (.*?) Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/#findComment-772971 Share on other sites More sharing options...
nrg_alpha Posted February 28, 2009 Share Posted February 28, 2009 Killah, to better understand the pitfalls of .+ or .* , you can have a look at this thread. CV and I explain the issue with those (in particular, reply #11 and #14), and instead use lazy quantifiers. Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/#findComment-773328 Share on other sites More sharing options...
killah Posted February 28, 2009 Author Share Posted February 28, 2009 Thank's for that link nrg_alpha, i have book marked it. And thanks CV, that worked. What would u say is best? ereg_xxx or preg_xxx for future referance? Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/#findComment-773473 Share on other sites More sharing options...
.josh Posted February 28, 2009 Share Posted February 28, 2009 The posix regex engine and syntax (what ereg_xxx uses) is generally thought to be simpler to use, but not offer you as much control over what gets matched and how. The pcre engine and syntax (what preg_xxx uses) is generally thought to have a higher learning curve, but the return on that is being able to write more complex patterns. Also, pcre is perl compatible (hence the engine name: Perl Compatible Regex Engine), meaning more pattern portability. Overall they mostly have the same syntax. There are some differences here and there, most of them are how things are matched though. Most of the time people generally don't care or need something complicated or portable enough to warrant having to pick one over the other. But what might be slightly more relevant for average joe user is that the posix functions (ereg_xxx) will no longer be part of php's core package as of php6, meaning, if you write your scripts using ereg_xxx functions and plan on having them around a long time, you're going to have to install a separate package when (if) you upgrade your php. Quote Link to comment https://forums.phpfreaks.com/topic/146968-how-would-i-get-this-to-work/#findComment-773494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.