PDA

View Full Version : calling functions in ereg()



coffeedemon
12-10-2003, 06:56 PM
this will match "[name=coffeedemon]" and i want to attempt it to call a function "displayName()" with coffeedemon as the argument. Can i do this?

ereg_replace("(\[name=)([a-zA-Z0-9]{3,20})(\])",displayName("\\2"),$str);

displayName() is something along the lines of

function displayName($name)
{
// name
$newName = ereg_replace('('. $name .')', "<a href=\"http://\\1.domain.com/\">\\1</a>", $name);



$result = $newName;

return $result;
}

mordred
12-10-2003, 08:30 PM
I think you're reaching beyond the capabilities of ereg_replace() in this context. The ereg_* functions are very basic regex functions compared to the much more powerful preg_* functions.

In your case, you can accomplish that by using either preg_replace() and the "e" modifier in your regex, or by calling preg_replace_callback().

For the first possibility, got the documentation page (http://us2.php.net/manual/en/function.preg-replace.php) and look at example 4 to get an idea how the "e" modifier should be used.

The second possibility is more straightforward to use IMO, because it doesn't rely on those cryptic modifiers. Take your pick.