faqts : Computers : Programming : Languages : PHP : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

32 of 60 people (53%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How do I replace [URL=http://www.whatever.com]Link[/URL] with anchor tags?

Jul 9th, 2003 12:34
Gerhardt, Andrew McFague,


<?php
$text = "Some link: [URL=http://www.example.com/]Link[/URL]";
/*
You didn't say if the link-description could contain
square-brackets like "[". So I had to make it relativly general
*/
$pattern = '#\[URL\s*=\s*(\w+://[^\]]+)\](.+?)\[/URL]#';
$replacement = '<a href="\1">\2</a>';
$text = preg_replace($pattern,$replacement,$text);
echo $text;
?>