[Let's hope phorum doesn't mangle the backslashes here. They're important. In the examples below, you should see exactly TWO backslashes before each number.]
Ronald, the regex documentation (type "man 7 regex" at your favorite Linux shell prompt) holds the key. It is written by and for space aliens*, and one of the most mysterious parts covers "parenthesized subexpressions." That's the magic you need.
Here's how it works.
Put parentheses around your regular expression. Since you're looking for a literal string -- nothing complicated here -- your expression would change from this:
"joe"
to this:
"(joe)"
Actually, it would look like "($somevar)" in your PHP code.
It matches exactly like the original, but there's an interesting side effect. The regex routines put the value of the match -- not the regular expression, but the found value -- into an internal string called a register.
You then can refer to that value in your replacement string as "\1" ... and voila, out pops the found value.
Try this:
<?
$str="<P>The quick joe brown fox JOE jumped over the lazy Joe dog's back.<P>";
echo $str;
$str = eregi_replace("(joe)","<b>\1</b>", $str);
echo $str;
?>
The reason they're called subexpressions is that you can have more than one of them, and not surprisingly, you can refer to them as "\2," "\3," etcetera.
Here's an example:
<?
$str="<P>The quick joe brown fox JOE jumped over the lazy Joe dog's back.<P>";
echo $str;
$str = eregi_replace("(j)(o)(e)","<b>\1</b><i>\2</i><blink>\3</blink>", $str);
echo $str;
?>
--
* actually the regex page in section 7 of the manual was written by Henry Spencer, who is not a space alien, but is a Canadian who wants to be in space. Close.