note 34876 deleted from function.preg-replace-callback by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 12:25:45 +0000 |
| Subject: | note 34876 deleted from function.preg-replace-callback by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57449@lists.php.net to get a copy of this message | ||
Note Submitter: tomer@parity-bit.com
----
Occasionally you will want to replace all HTML links with a printable URL and the label - for
example, perhaps a wikipedia.
This worked fine for me:
preg_replace('/\<A
HREF="?([^">]+)"?\>(.*)<\/A\>/U','<I>$2</I>
[$1]',$input);
so <A HREF="http://www.example.com">Place</A> becomes
<I>Place</I> [http://www.example.com]
It will work without the quotes around the URL but not if the A tag has extra properties. It also
keeps HTML formatting - so
<A HREF="place"><B>Universe</B></A> becomes
<I><B>Universe</B></I> [place]
Be warned - doing <A><B></A></B> is a bad idea generally, and here the
</B> tag will not be picked up.