Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 9th, 2007, 03:59 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 249
Thanks: 0
Thanked 0 Times in 0 Posts
Default preg_replace problem

Can somebody please help me with what is happening here. I haven't used regular expressions much.

I have a string that is used to set up a link, [Link]http://www.somelink.com, my link text[/Link], within some content. I am needing to grab just the "my link text" for use elsewhere at one point. What is happening is that if I put my link tags in more than the results are the pattern is continuing to the last [/Link] tag and then remove some content. I just need to know how to get it to stop after the first [/Link] tag, I thought the .

$pattern = '@\[Link][^"]+,\ ?(.*?)\[/Link\]@';
$replacement = '\\1';
$strString2 = preg_replace($pattern, $replacement, $strString2);

$strString2 = "one [Link]http://www.linktwo.com, two[/Link] three four five six seven eight nine ten eleven";
// Results with one set of [Link] tags
//one two three four five six seven eight nine ten eleven

$strString2 = "one [Link]http://www.linktwo.com, two[/Link] three four five six [Link]http://www.linkseven.com, seven[/Link] eight nine ten eleven";
// Results with more than one set of [Link] tags
//one seven eight nine ten eleven

Thank You

Similar Threads
Thread Thread Starter Forum Replies Last Post
Preg_replace not stripping exclamation marks Picabrillo Pro PHP 0 February 8th, 2007 01:30 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.