I have search around and only found replacement options that were not specific to the need I have below.
However, the closest thing I found was this:
preg_replace('/<a(.*)title="([^"]*)"(.*)>/','chocolate chip cookies',$items);
I think an answer to this question may also help others with similar needs.
Simply, I want to globally replace in one statement if possible, specific word(s) in all the title attributes (title="") with other word(s). This should work for both hyperlink and image title attributes.
For example: I would line to change the words "chocolate", "chocolate cookies", and "chocolate vanilla cookies" to "chocolate chip cookies" within all the title attributes as shown in the examples below.
However, I DO NOT want to change the word "chocolate" in either the URL or the link's text.
There are a total 50 items listings with 50 individual hyperlinks, all placed in a single array. So preferably, the replacement code should work for replacing the word(s) in the entire array in one statement.
Note: If Example 3 is too difficult, then an answer for Examples 1 and 2 will be sufficient.
Example 1:
<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate">chocolate</a>
Example 2:
<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate cookies">chocolate</a>
Example 3:
<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate vanilla cookies">chocolate</a>
Desired Result:
<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate chip cookies">chocolate</a>
Thank you.
You could use for example DomDocument with
getElementsByTagNameto find the elements and test the value of the title attribute using a pattern:Explanation
\bWord boundarychocolateMatch literally(?:Non capture group(?:\hvanilla)?Optionally match a horizontal whitespace char and vanilla\hcookiesMatch a horizontal whitespace char and cookies)?Close group and make it optional\bWord boundaryRegex demo | Php demo
If the pattern matches, then you can set the new title.
Output