Jump to content

preg_replace() help


Anman

Recommended Posts

Hey, I'm trying to create a syntax highlighter plugin using GeSHi.

 

My only problem is this:

 preg_replace('#\[code=(.*?)\](.*?)\[/code\]#i', "<div class='codeblock'>" . geshify("$1", "$2") . "</div>", $message); 

 

In particular:

geshify("$1", "$2")

 

The problem is it's passing literally dollar sign 1 and 2 (respectively) to the function, not the replacement.

 

I would greatly appreciate any help regarding this matter.[/code]

Link to comment
https://forums.phpfreaks.com/topic/178396-preg_replace-help/
Share on other sites

If you want to run the function and use its output in the replacement, you would have to use preg_replace_callback() e.g.:

 

<?php
$message = preg_replace_callback(
'#\[code=(.*?)\](.*?)\[/code\]#i',
create_function(
	'$matches',
	'return \'<div class="codeblock">\' . geshify($matches[1], $matches[2]) . \'</div>\';'
),
$message
);
?>

Link to comment
https://forums.phpfreaks.com/topic/178396-preg_replace-help/#findComment-940773
Share on other sites

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.