bachx Posted July 31, 2007 Share Posted July 31, 2007 Hello, I've made a BB-code script, but I'm wondering about one thing. I want to have a maximum allowed size when using . Here is a sample code snippet of my bbcode script: function format_bbcodes($text) { $text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is",'<span style="font-size: $1px">$2</span>',$text); return $text; } Quote Link to comment https://forums.phpfreaks.com/topic/62639-bb-codes-question/ Share on other sites More sharing options...
mrjcfreak Posted July 31, 2007 Share Posted July 31, 2007 function format_bbcodes($text) { return preg_replace("(\[size=(.+?)\](.+?)\[\/size\])ies","'<span style=\"font-size: '.max($1,20).'px\">$2</span>'",$text); } Works quite well... Adding the 'e' switch means the replacement is treated as a PHP expression, hence being able to put a bit of code in there... if you wanted to do further validation, you could use: function splat($size =10) { return ($size > 20) ? 20 : $size; } function format_bbcodes($text) { return preg_replace("(\[size=(.+?)\](.+?)\[\/size\])ies","'<span style=\"font-size: '.max($1,20).'px\">$2</span>'",$text); } Where splat has extra checks applied to it. Swapping the size=(.+?)\ for size=(\d{1,2})\ would be a good plan Quote Link to comment https://forums.phpfreaks.com/topic/62639-bb-codes-question/#findComment-311773 Share on other sites More sharing options...
bachx Posted July 31, 2007 Author Share Posted July 31, 2007 Sadly, the above code didn't work. It's always giving me a font size of 20, whether the size is more or less that 20. I think it's the max() function, it should be something else. Quote Link to comment https://forums.phpfreaks.com/topic/62639-bb-codes-question/#findComment-312236 Share on other sites More sharing options...
mrjcfreak Posted August 1, 2007 Share Posted August 1, 2007 Duh, yes, silly me, it should obviously be the min function! To be a bit nicer, use: max(min($1,20), which will make sure all the values are between 8 and 20 Quote Link to comment https://forums.phpfreaks.com/topic/62639-bb-codes-question/#findComment-312631 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.