PHP function inside a regex - possible?
| From: | kev | Date: | Thu, 01 Jun 2000 16:55:42 +0000 |
| Subject: | PHP function inside a regex - possible? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-829@lists.php.net to get a copy of this message | ||
Hi,
I want to take a block of HTML-ised text, say for example:
<B>Steve McManaman</B>'s Wembley return has been accompanied by
Hollywood hype but his adequate midfield display in
the 2-0 win over Ukraine, packed with English endeavour rather
than Spanish flair, will stem the fervour.
That won't necessarily be a bad thing for <B>Kevin Keegan</B> who
knows there will be days when McManaman scatters a
little magic to ice a performance like this.
.. and I want to replace all the words or phrases in bold with links.
For example the above text would become:
<B><A href=search.php?query=Steve+McManaman>Steve McManaman</A></B>'s
Wembley return has been accompanied by Hollywood hype but his adequate
midfield display in
the 2-0 win over Ukraine, packed with English endeavour rather
than Spanish flair, will stem the fervour.
That won't necessarily be a bad thing for <B><A
href=search.php?query=Kevin+Keegan</A>Kevin Keegan</B> who knows there
will be days when McManaman scatters a
little magic to ice a performance like this.
...but without being able to execute PHP functions from inside a regex
this wouldn't be possible. The closest I can get is:
preg_replace( "|<B>(.*)</B>|", "<A
href=search.php?query=$1><B>$1</B></A>", $text );
or something to that effect. But of course, the first $1 needs some
processing done on it first, to urlencode it (and probably add quotes
round phrases).
Is this possible with a regex solution?
thanks,
- Kev