JJ2K Posted September 21, 2010 Share Posted September 21, 2010 Hi, I have a link like this: <a href="myword.php" title="myword">some other text myword blah foo</a> OK now I need to replace myword with newword, but only in the anchor text. So myword in the href or title should not be changed, only matches found inside the anchor text! I tried using str_replace but obviously this just matched all occurances. So I guess preg_replace is the way to go here but it is far too advanced for me and I cannot get my head around all the different symbols. Here is some example code: $html = '<a href="myword.php" title="myword">some other text myword blah foo</a>'; $new = 'newword' $html = preg_replace(//unsure here!); I just don't know how to work regex OT: Did you find it easy/difficult to learn? Thanks to any help Quote Link to comment Share on other sites More sharing options...
ShibSta Posted September 22, 2010 Share Posted September 22, 2010 Regular Expressions would have to be one of my weaker links... However, I would imagine something like this (posted below) would suffice. preg_replace('/(?<=title=\")(.*)(\"=>?)/', 'apple', $haystack); You and I both should take some time to read about the PCRE regular expression syntax. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.