Page 1 of 1

RegEx to find all text not in bbCode tags

Posted: Thu Feb 01, 2007 1:32 pm
by RobertGonzalez
Ok, I admit it, I am totally not strong in RegEx. I need some help and I cannot seem to figure this out at all.

Say I have a string like this (sorry for the code tag, but the PHP tag was making this little string puke three ways from sideways)...

Code: Select all

<?php
$string = 'Here is some code
[php]<?php
// Run them through the function
if (!is_value_set($checks, $_POST))
{
    // Error out
}
?>[/php]

I hope that helps

[php]<?php
$more = \'A little more code\';
?>[/php]';
?>
I have all the jazz working for grabbing all the stuff in the [ php ] tags and doing what I want with that. What I want now is to be able to take all the text that is not in between [ php ] and [ /php ] tags and manipulate it. How in the world do I do that?

Posted: Thu Feb 01, 2007 3:58 pm
by Christopher
I am really not clear on what you want to do. Could you provide some example text you want to parse ... or is that it?

Posted: Thu Feb 01, 2007 4:36 pm
by RobertGonzalez
Imagine this string is coming from a database call:

Code: Select all

THIS IS SOME REGULAR CONTENT TYPE TEXT STUFF

[php]<?php
// Run them through the function
if (!is_value_set($checks, $_POST))
{
    // Error out
}
?>[/php]

THIS IS SOME MORE REGULAR CONTENT TYPE TEXT STUFF

[php]<?php
$more = \'A little more code\';
?>[/php]
In this string, I want to be able to take everything that is inside the [ php ] tags and turn them into highlighted PHP code. That parts is done and working. The problem I am having now lies in the non tag-wrapped text. There is no markup surrounding that text and I want to be able to wrap the left over, non-tag-wrapped text in <p></p> tags to make for compliant markup in the display. So what I am after is a regex (or other mechanism) that would be able to grab all text that is not wrapped in a bbCode tag and set it aside so I can run some other manipulation on it to make it compliant mark up.

Does that make sense?

Posted: Fri Feb 02, 2007 9:05 am
by feyd
The quick and dirty is

Code: Select all

function highlightMe($a)
{
  return '<div>My PHP:</div><pre>'.highlight_string(stripslashes($a[1])).'</pre>';
}

$foo = preg_replace_callback('#\[php'.'\](.*?)\[/'.'php\]#is', 'highlightMe', $foo);
That's untested, but should only work for non-nested sets.

Posted: Fri Feb 02, 2007 10:17 am
by RobertGonzalez
I am using something like that now that handles the highlighting and such. My challenge is when there is more than one block of code or text in the string that needs to be run through.

I was thinking of actually using substitution to remove the code blocks, then HTML-ize the remaining text, then substitute back in the CODE that was removed. But that seems unnecessary if there was a regex that could handle the text that is not wrapped in code tags.

Thanks for the input through. It is much appreciated.

PS There is a nifty highlighter I am using that is really pretty cool. It is lightweight, one class and has a built functionality for parsing PHP functions into links. If anyone is interested I snagged it from http://web.archive.org/web/200605021523 ... hlight.php.

Also, a la feyd's example, here are a couple of bbCode type parser functions, in case you want one...

This one came from the PHP Manual

Code: Select all

<?php
function bb_php($str){
	$str = str_replace("]\n", "]", $str);
	$match = array('#\[php\](.*?)\[\/php\]#se');
	$replace = array("'<div>'.highlight_string(stripslashes('$1'), true).'</div>'");
	return preg_replace($match, $replace, $str);
}

echo bb_php($string);
?>
Using the highlighter code I posted the link to above...

Code: Select all

<?php
function parse_callback($array)
{
	require_once 'PHP_Highlight.php';
	
	$h = new PHP_Highlight;
	$h->loadString($array[1]);
	return $h->toHtmlBlocks(true);
}

echo preg_replace_callback($match, 'parse_callback', $string);
?>
In both cases, $string is the string that contains the text/PHP mash that you want to bbCode parse.