Dragen Posted June 2, 2008 Share Posted June 2, 2008 Okay, I'm trying to create a bbcode function, which I've got working perfectly except for the code box. At the moment I'm using this regex for it: (\[code\](.+?)\[/code\])s replacing it with this: <div class="code"><div class="head">code:</div><pre>$1</pre></div> Which works fine. The problem is that if the code text has any bbcode formatting inside it, it still formats it. For example: [code]hello this is [b]bold[/b] and this is a [link=google.com]link[/link] [/code] would create the code box, but also format the bold text and create the url link. How would I go about displaying them as text inside the code box, instead of formatting them, like it is with this website? Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/ Share on other sites More sharing options...
Dragen Posted June 2, 2008 Author Share Posted June 2, 2008 any help? Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-555795 Share on other sites More sharing options...
soycharliente Posted June 2, 2008 Share Posted June 2, 2008 Use htmlspecialchars on it. Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-555939 Share on other sites More sharing options...
Dragen Posted June 2, 2008 Author Share Posted June 2, 2008 htmlspecialchars isn't going to stop the bbcode being changed to html though. For example any [b]hello[/b] inside of a code tag will be changed to: <b>hello</b> unlike on this forum where it doesn't change bbcode placed within a code tag Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-555989 Share on other sites More sharing options...
wildteen88 Posted June 2, 2008 Share Posted June 2, 2008 I'd create a dedicated function to parse code tags. I'd then use str_replace to convert [ and ] to their html entities equivalent.  function parse_code($code) {   $code = str_replace(array('[', ']'), array('& #91;', '& #93;'), $code); // remove the space between & and #. The forum messes this up   $output = '<div class="code"><div class="head">code:</div><pre>' . $code . '</pre></div>';   return $output; } $text = '[b]Bold test[/b] [code]Add code here no [b]bbcode[/b] should get parsed ';  // replace code tags first $text = preg_replace('|\ [code\](.+?)\[/code\]|es', "parse_code('$1')", $text); $text = preg_replace('|\[b\](.+?)\[/b\]|s', '<b>$1</b>', $text); echo $text;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-556009 Share on other sites More sharing options...
Dragen Posted June 4, 2008 Author Share Posted June 4, 2008 thanks for the idea wildteen. I've been mucking about with that idea, but it seems as thouygh you can't pass a function through the replacement in preg_repace. If I use something like this: <?php function replace($text){ return str_replace(array('[', ']'), array('& #91;', '& #93;'), $text); } $text = '[ code]Add code here no [b]bbcode[/b] should get parsed[ /code]'; $text = preg_replace('(\[code\](.+?)\[/code\])s', "replace('$1')", $text); ?> Â what it actually outputs is this: ('Add code here no [b]bbcode[/b] should get parsed') Not it removes the function name, but keeps the brackets and ' for it and also doesn't call the function itself. I've tried several different variations of calling the function, including: $text = preg_replace('(\[code\](.+?)\[/code\])s', replace("$1"), $text); but that just passes the literal value $1 and not the preg_replace $1. Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-557386 Share on other sites More sharing options...
Dragen Posted June 4, 2008 Author Share Posted June 4, 2008 Okay I've got it working without using a separate function and a couple of preg_replaces: <?php function create($text, $order = 'id'){ $text = nl2br(htmlentities(trim($text))); if(is_array($codes = $this->collect())){ //this collects the bbcode regex from a database foreach($codes as $code){ //loops through each regex if($code['title'] == 'formatted text'){ //if it's the [code] then... preg_match_all($code['regex'], $text, $return, PREG_PATTERN_ORDER); //finds all the text within code blocks if(isset($return[1]) && is_array($return[1])){ for($i = 0; $i < count($return[1]); $i++){ $r[1] = str_replace(array('[', ']'), array('[', ']'), $return[1][$i]); //then replaces all of the bbcode brackets inside $r[0] = str_replace(array('[', ']'), array('\[', '\]'), $return[1][$i]); //and adds slashes to the found text so I can use it in a regex to replace the text $text = preg_replace('|(\[code\])('.$r[0].')(\[/code\])|', '$1'.$r[1].'$3', $text, -1, $c); } } } $text = preg_replace($code['regex'], $code['replace'], $text); } }else{ $text = 'ERROR COLLECTING BBCODE!<br />' . $text; } return $text; } ?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-557505 Share on other sites More sharing options...
kev wood Posted June 4, 2008 Share Posted June 4, 2008 dont know if this will help but this is BBcode that works i found it on a post on the very last page on the php help. i got bored waiting for an answer so i was looking if anyone else had the same problem as me and found this if it dont help sorry for wasting your time but if it does at least i may have helped you out.  function PHPEncode($String) {  preg_match_all("|\[code\].+?\[/code\]|ims", $String, $matches);  $phpOld = array();  $phpNew = array();  foreach ($matches[0] as $orig) {   $phpOld[] = $orig;   $new = str_replace(array('<','>','<br />','<br>'), array('<','>',"\n","\n"), $orig);   $new = trim(preg_replace("|\[code\](.+?)\[/code\]|ims", "$1", $new));   $phpNew[] = "<div class='snippetTitle'>PHP Code:</div>\n<div class='codeSnippet'>" . highlight_string($new, true) . "</div>\n";  }  $String = str_replace($phpOld, $phpNew, $String);      return $String; }  you clicked solved before i finished so i guess it dont matter now anyway Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-557511 Share on other sites More sharing options...
Dragen Posted June 4, 2008 Author Share Posted June 4, 2008 thanks kev. It's similar to what I'm now using, although it does something quite different (mainly replacing < and > instead of [ and ]). I think my code is better for my needs and works fine. Thanks for the info though! Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-557528 Share on other sites More sharing options...
kev wood Posted June 4, 2008 Share Posted June 4, 2008 sound i have not tried to do what you are at the min so i was unsure if it could help you but it might of done so i thought i would put it up for you. if i can help i will try if i dont know i will have a go. Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-557541 Share on other sites More sharing options...
wildteen88 Posted June 4, 2008 Share Posted June 4, 2008 thanks for the idea wildteen. I've been mucking about with that idea, but it seems as thouygh you can't pass a function through the replacement in preg_repace. If I use something like this: <?php function replace($text){ return str_replace(array('[', ']'), array('& #91;', '& #93;'), $text); } $text = '[ code]Add code here no [b]bbcode[/b] should get parsed[ /code]'; $text = preg_replace('(\[code\](.+?)\[/code\])s', "replace('$1')", $text); ?> Â what it actually outputs is this: ('Add code here no [b]bbcode[/b] should get parsed') Not it removes the function name, but keeps the brackets and ' for it and also doesn't call the function itself. I've tried several different variations of calling the function, including: $text = preg_replace('(\[code\](.+?)\[/code\])s', replace("$1"), $text); but that just passes the literal value $1 and not the preg_replace $1. That's because you have to use the e regex modifier (at the end of your regex pattern): $text = preg_replace('(\[code\](.+?)\[/code\])es', replace("$1"), $text); Aslo make sure you parse your code bbcodes before any other bbcodes. Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-557670 Share on other sites More sharing options...
Dragen Posted June 4, 2008 Author Share Posted June 4, 2008 oh, okay. So does the e modifier just allow the use of functions and things then? Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-557971 Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 Yes. PHP will parse the code in the replacement parameter. Without the modifier PHP will teat the code in the replacement as text. Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-558483 Share on other sites More sharing options...
Dragen Posted June 5, 2008 Author Share Posted June 5, 2008 brilliant! thank you. Do you know of any sites that list all of the regex modifiers? I've tried googling and didn't really find anything. Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-558714 Share on other sites More sharing options...
soycharliente Posted June 6, 2008 Share Posted June 6, 2008 http://www.regular-expressions.info/ Â An amazing tutorial and reference guide. Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-558911 Share on other sites More sharing options...
Dragen Posted June 6, 2008 Author Share Posted June 6, 2008 thanks. Looks good, I'll definitely bookmark it Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-559349 Share on other sites More sharing options...
wildteen88 Posted June 7, 2008 Share Posted June 7, 2008 brilliant! thank you. Do you know of any sites that list all of the regex modifiers? I've tried googling and didn't really find anything. The manual is a good place too. Quote Link to comment https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/#findComment-559747 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.