Page 1 of 1
preg_replace with function output
Posted: Mon Aug 22, 2005 3:36 pm
by jwalsh
Hi,
Trying to realize this php code. Should be trivial, but I'm not sure what I'm doing wrong here.
Code: Select all
function bbcode($string){
$patterns[0] = '`\[b\](.+?)\[/b\]`is';
$patterns[1] = '`\[i\](.+?)\[/i\]`is';
$patterns[2] = '`\[u\](.+?)\[/u\]`is';
$patterns[3] = '`\[quote\](.+?)\[/quote\]`is';
//$patterns[4] = '`\[php\](.+?)\[/php\]`is';
$replaces[0] = '<strong>\\1</strong>';
$replaces[1] = '<em>\\1</em>';
$replaces[2] = '<span style="border-bottom: 1px dotted">\\1</span>';
$replaces[3] = '<strong>Quote:</strong><div style="margin:0px 10px;padding:5px;background-color:#9CD172#5a8c32;border:1px dotted #5A8C32;width:80%;"><em>\1</em></div>';#9cd172
//$replaces[4] = "'" . makephp($1) "'";
$string = preg_replace($patterns, $replaces , $string);
return $string;
}
?>
It works perfect except for the array lines that are commented. If I uncomment those, I get a parse error.
Thanks,
Josh
Posted: Mon Aug 22, 2005 3:47 pm
by Sander
You forgot a dot (.) in that line:
Code: Select all
$replaces[4] = "'" . makephp($1) . "'";
Posted: Mon Aug 22, 2005 4:11 pm
by jwalsh
I've fixed that, dont know how I over looked that, but I still have the same problem.
Code: Select all
Parse error: parse error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /home/nibuhadn/public_html/modules/inquiries/functions.php on line 267
Posted: Mon Aug 22, 2005 4:22 pm
by timvw
I believe you would have to use the "e" modifier. Or you could use preg_replace_callback.
Posted: Mon Aug 22, 2005 4:34 pm
by jwalsh
Good call on the e modifier. but it's not actually calling the function, just regurgitating the value of 1.
Anything else here? I'm stumped. I've switched to temporarily using highlight_string instead of my function for now, just for testing reasons.
Code: Select all
function bbcode($string){
$patterns[0] = '`\[b\](.+?)\[/b\]`is';
$patterns[1] = '`\[i\](.+?)\[/i\]`is';
$patterns[2] = '`\[u\](.+?)\[/u\]`is';
$patterns[3] = '`\[quote\](.+?)\[/quote\]`is';
$patterns[4] = '`\[php\](.+?)\[/php\]`e';
$replaces[0] = '<strong>\\1</strong>';
$replaces[1] = '<em>\\1</em>';
$replaces[2] = '<span style="border-bottom: 1px dotted">\\1</span>';
$replaces[3] = '<strong>Quote:</strong><div style="margin:0px 10px;padding:5px;background-color:#9CD172;border:1px dotted #5A8C32;width:80%;"><em>\1</em></div>';
$replaces[4] = 'highlight_string(1)';
$string = preg_replace($patterns, $replaces , $string);
return $string;
}
Posted: Mon Aug 22, 2005 4:48 pm
by feyd
forgot \\ in front of 1, and needs to be in quotes of some sort to make sure.. however, I'd recommend preg_replace_callback(), as it's more secure.
Posted: Mon Aug 22, 2005 4:59 pm
by jwalsh
I apologize, but I am very confused... I'm getting very strange output.... I'm very new to preg_replace and callback.
Code: Select all
function bbcode($string){
$patterns[0] = '`\[b\](.+?)\[/b\]`is';
$patterns[1] = '`\[i\](.+?)\[/i\]`is';
$patterns[2] = '`\[u\](.+?)\[/u\]`is';
$patterns[3] = '`\[quote\](.+?)\[/quote\]`is';
$patterns[4] = '`\[php\](.+?)\[/php\]`e';
$replaces[0] = '<strong>\\1</strong>';
$replaces[1] = '<em>\\1</em>';
$replaces[2] = '<span style="border-bottom: 1px dotted">\\1</span>';
$replaces[3] = '<strong>Quote:</strong><div style="margin:0px 10px;padding:5px;background-color:#9CD172;border:1px dotted #5A8C32;width:80%;"><em>\1</em></div>';
$replaces[4] = 'highlight_string(\\1)';
$string = preg_replace($patterns, $replaces , $string);
return $string;
}

Posted: Mon Aug 22, 2005 5:23 pm
by feyd
Code: Select all
$text = preg_replace_callback('#\[php\](.*?)\[/php\]#i', create_function('$a','return highlight_string($a[1]);'),$text);
Posted: Mon Aug 22, 2005 5:34 pm
by jwalsh
LOL, I'm about to give up. For testing reasons, I removed all the other bbcode (which works fine), and am worrying only about the php.
The output of the following is identical to the image above...
Code: Select all
function bbcode($string){
$string = preg_replace_callback('#\[php\](.*?)\[/php\]#i', create_function('$a','return highlight_string($a[1]);'),$string);
return $string;
}
should there be a \\ in front of that 1?
Posted: Mon Aug 22, 2005 5:47 pm
by feyd
you'll need to use the function created to inspect the code to see if it needs to surround the string in php's start/end markers <?php ?>
basically like this...
Code: Select all
$string = preg_replace_callback('#\[php\](.*?)\[/php\]#i', create_function('$a','return highlight_string(preg_match("#<\?php.*?\?>#s",$a[1]) ? $a[1] : "<?php\n{$a[1]}\n?>");'),$string);
Posted: Mon Aug 22, 2005 6:01 pm
by jwalsh
How can something this simple be so frustrating. Thanks Feyd for all your help, it's very appreciated. I've simplified the output it's trying to display, just to see if it's a addslashes or stripslashes problem. It's just outputting as plain text. Ultimately, this will run through a different function.
One last time (before I just ditch this completely)... Here's my code
Code: Select all
function bbcode($string){
$string = preg_replace_callback('#\[php\](.*?)\[/php\]#i', create_function('$a','return highlight_string(preg_match("#<\?php.*?\?>#s",$a[1]) ? $a[1] : "<?php\n{$a[1]}\n?>");'),$string);
return $string;
}
and here's the output (plain text) It's not even removing the
Code: Select all
that was entered, so I'm wondering if my regular expression is correct.:
Code: Select all
[php]
echo "testing 123";
if ($working == 1) {
echo "it worked";
} else {
echo "it didn't work";
}
[/php]
Posted: Mon Aug 22, 2005 6:11 pm
by feyd

may want to add a 's' modifier to the pattern
Posted: Mon Aug 22, 2005 6:19 pm
by jwalsh
Ok, we are now getting somewhere.... Very Close. Thanks Feyd.
It's putting the output in the wrong place on the page, and putting a "1" in the place of where the output should be. My guess is the preg_replace is returning a boolean value.
I'm not exactly sure why it's putting the highlight_file in the wrong place, but since my function doesn't display anything, just returns results, it's probably ok.