Page 1 of 1

[SOLVED] preg_replace, including a function...

Posted: Fri Aug 22, 2003 4:20 pm
by JAM
I have an issue that I just cant work out. I tried different ways, but I keep getting stuck.

A part of the script parses text to replace emails, with email-links. That works. But I would like to embed a email-harvester-blocker by decoding the emails, using a function. If the user clicks an email, he/shes is sendt to email.php that decodes the mail and so on...

I have these in the script...

Code: Select all

<?php
$patterns[]	= "#\[email\](.*?)\[/email\]#si";
$replacements[] = '<a href="mailto:\1">\1</a>';
		
$patterns[]	= "#\[email=(.*?){1}(.*?)\](.*?)\[/email\]#si";
$replacements[] = '<a href="mailto:\1\2">\3</a>';

$message = preg_replace($patterns, $replacements, $message);
?>
...among other stuff. It works good, but I'm trying to add a email-harvester-blocker, by using the function mailencode($mail). So...

Code: Select all

<?php
// something like this, tho the below obviously wont work...
$patterns[]	= "#\[email=(.*?){1}(.*?)\](.*?)\[/email\]#si";
$replacements[] = '<a href="email.php?mail='.mailencode(\\1\\2).'">\3</a>';

//^--- edited the \1\2 doesn't show right in this snippet for some reason...

?>
Ideas welcome.

Posted: Fri Aug 22, 2003 4:47 pm
by volka
I guess you found the modifier e for preg_replace
If this modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string.

Only preg_replace() uses this modifier; it is ignored by other PCRE functions.
and tries to use it.
You did it almost right but
$replacements[] = '<a href="email.php?mail='.mailencode(\1\2).'">\3</a>';
is evaluated before it reaches preg_replace. You have to quote it in a way that php knowns, that you want your code as string-parameter to preg_replace, i.e. if you print the string the output has to look like the code you want to be executed (except the subpattern-replacements).
Only the function executing it would be

Code: Select all

<?php
// only a dummy function, not producing useful output
function mailencode($a, $b)
{
	return ""$a" ". strrev($b);
}


$subject = '[email=1rsu]User #1[/email]';


$patterns = array();
$replacements = array();


$patterns[]   = "#\[email=(.*?){1}(.*?)\](.*?)\[/email\]#sie";
$replacements[] = 'mailencode("$1", "$2");';

echo 'take a look at this ', $replacements[0], "\n";
print_r(preg_replace($patterns, $replacements, $subject));
?>
Now you have to put back the string before and after the function-result. But remember: it must be a string containing the code to be executed.

Code: Select all

<?php

function mailencode($a, $b)
{
	return ""$a" ". strrev($b);
}


$subject = '[email=1rsu]User #1[/email]';


$patterns = array();
$replacements = array();


$patterns[]   = "#\[email=(.*?){1}(.*?)\](.*?)\[/email\]#sie";
$replacements[] = '''<a href="email.php?mail=''.mailencode("$1", "$2").''">\3</a>'';';

echo 'take a look at this ', $replacements[0], "\n";

print_r(preg_replace($patterns, $replacements, $subject));
?>

Posted: Fri Aug 22, 2003 5:32 pm
by JAM
You have to quote it in a way that php knowns
I got to the point that I figured that out, but I had no idea whatsoever on how to... The php parser kept bugging me about dots, ''s and other chars.
I even tried something using ${0}, but I have no idea on wheither its something you can use in this area... Oh well...

Thanks for the example(s). Made a whole lot easier, even if I know foobar about preg/ereg.