Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/108398-solved-bbcode-code-box-help/
Share on other sites

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]

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.

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('&#91;', '&#93;'), $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]

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.