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 Link to comment https://forums.phpfreaks.com/topic/214043-simple-preg_replace-help-please/ 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. Link to comment https://forums.phpfreaks.com/topic/214043-simple-preg_replace-help-please/#findComment-1113916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.