Find & Replace

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Find & Replace

Post by cbrian »

I want to replace something like [link=http://www.site.com]Go here![/link]
with <a href="http://www.site.com">Go here!</a>

Can I do that with Regular Expressions?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes. I'm pretty sure I posted the code for it a while ago in here somewhere....



well.. if you look at phpbb's bbcode.php file in the include folder, you'll find the info packed in there... There was a half-decent evilwalrus bbcode thing, but since the server is in limbo... you may be able to snag it via google cache or something.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$string = '[link=http://www.site.com]Go here![/link]';

$anchor = preg_replace('/\[link=([^\]]+)\]([^\[]+)\[\/link\]/i', "<a href=\"$1\">$2</a>", $string);
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Thanks, both of you!
Post Reply