|
Posted by Chung Leong on 09/13/05 23:48
That doesn't work, because the pattern will match again as soon as the
regexp engine looks one letter to the right.
The bracketed letter below indicates the first letter of a possible
match. The following fails the lookbehind assertion, because "mailto:"
matches the condition:
mailto:[c]hernyshevsky@hotmail.com
The following does not fail the assertion, as "mailto:c" is not a
match:
mailto:c[h]ernyshevsky@hotmail.com
A job like this is easier with preg_replace_callback(), arguably the
most powerful text manipulation function in PHP. Example:
function antispam($m) {
return ($m[1]) ? $m[0] :
"<script>document.write('<a
href=\'mailto:'+'{$m[3]}'+'@'+'{$m[4]}'+'\'>'+'$m[3]'+'@'+'$m[4]'+'</a>');</script>";
}
preg_replace_callback('/(mailto:)?\s*(([_\\.0-9a-z-]+)@([_\\.0-9a-z-]+))/i',
'antispam', $string);
[Back to original message]
|