preg_replace with function output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

preg_replace with function output

Post 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
Sander
Forum Commoner
Posts: 38
Joined: Sat Aug 06, 2005 12:43 pm

Post by Sander »

You forgot a dot (.) in that line:

Code: Select all

$replaces[4] = "'" . makephp($1) . "'";
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I believe you would have to use the "e" modifier. Or you could use preg_replace_callback.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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;
}
Image
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$text = preg_replace_callback('#\[php\](.*?)\[/php\]#i', create_function('$a','return highlight_string($a[1]);'),$text);
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:oops: may want to add a 's' modifier to the pattern
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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.
Post Reply