By learning the basics of regular expressions.
Search the net for some tutorials.
$a="<a href=\"http://abc.php\">abc</a>";
$b=preg_replace("/<a href=\"(.?)\">(.?)<\/a>/","\1 (\2)",$a);
echo "$a\n$b\n";
and the explenation: READ THIS PLEASE so you can learn something
"/<a href=\"(.+)\">(.+)<\/a>/"
WHat does this do?
The outer quote are just string quotes
The outer /'s define where the pattern to match starts and stops.
<a href=
This needs to be matched. if it doesn't exist in the string, there will be not match, no replace.
\"
Escaped quote, tells PHP not to interpret this quote as PHP code, but as a character that must be matched.
(.+)
brackets mean "treat what's in here as a whole, not as seperate items"
the dot means "any character" and the + means "one or more times.
So (.+) means, "this matches a string of a least one character".
The \" and > mean to match the real quote an bracket
then another (.+) to match whatever is between the > and the </a>
and finally a real </a> (with escape chars to make it real)
Then, in the target you back-reference the matches in the pattern.
Everything you put inside brackets (), like the (.+) can be back-referenced.
So when I put
"\1 (\2)"
The \1 is replaced with whatever matched the first set of () in the pattern,
and \2 is replaced with whatever matched the second ().
Note that here, the \ does not mean escape, it does not print a real "\" character.