Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 9th, 2007, 03:59 PM
Friend of Wrox
Join Date: Jun 2003
Location: Oklahoma City, Oklahoma, USA.
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
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 03:36 PM.

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