Jump to content

Simple preg_replace Help Please!


JJ2K

Recommended Posts

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  :shrug: 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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.