Jump to content

Calling a function on regular expressions?


jackpf

Recommended Posts

Hi all, first post.

Basically, my site has bbcode style regular expressions.

However, I was wondering if there's a way to call a function on an extracted regular expression?

For example,

 

<?php
function replacewithfunction($str)
{
	$exist = '/\[code lang\=(.*?)\](.*?)\[\/code\]/is';

	$replace = geshi_code($1, $2);

	$return = preg_replace($exist, $replace, $str);
	return $return;
}
?>

 

Where geshi_code() is the function I wish to call.

Obviously this returns a syntax error, but this is basically what I want do.

Any help would be much appreciated.

Thanks,

Jack.

Hmm...had a look. I've now got this-

<?php
function geshi_code($lang, $source)
{
	include('geshi/geshi.php');
	$path = 'geshi/geshi/';

	$geshi = new GeSHi($source, $lang, $path);
	return $geshi->parse_code();
}
function str_code($str)
{
	$str = htmlspecialchars($str);
	$exist = '/\[somecode lang\=(.*?)\](.*?)\[\/somecode\]/is';

	$replace = geshi_code("$1", "$2");

	return preg_replace($exist, $replace, $str);

}

echo str_code("This is some code.[somecode lang=php]<?php $var = 'hello'; ?>[/somecode] end of code.");
?>

But it returns this-

This is some code.

 

<?php $var = 'hello'; ?>

 

end of code.

 

Unhighlighted. In case you didn't know, geshi is a syntax highlighter.

I don't understand why it's not working...

 

Thanks anyway.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.