How to Replace Words in all the Title Attributes using PHP

155 Views Asked by At

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.

1

There are 1 best solutions below

1
The fourth bird On BEST ANSWER

You could use for example DomDocument with getElementsByTagName to find the elements and test the value of the title attribute using a pattern:

\bchocolate(?:(?:\hvanilla)?\hcookies)?\b 

Explanation

  • \b Word boundary
  • chocolate Match literally
  • (?: Non capture group
    • (?:\hvanilla)? Optionally match a horizontal whitespace char and vanilla
    • \hcookies Match a horizontal whitespace char and cookies
  • )? Close group and make it optional
  • \b Word boundary

Regex demo | Php demo

If the pattern matches, then you can set the new title.

$html = <<<HTML
<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate">chocolate</a>
<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate cookies">chocolate</a>
<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate vanilla cookies">chocolate</a>
HTML;

$doc = new DomDocument();
$fragment = $doc->createDocumentFragment();
$fragment->appendXML($html);
$doc->appendChild($fragment);
$nodes = $doc->getElementsByTagName('a');

foreach ($nodes as $node) {
    $node->setAttribute(
        "title",
        preg_replace("~\bchocolate(?:(?:\hvanilla)?\hcookies)?\b~", "chocolate chip cookies", $node->getAttribute("title")
        )
    );
}
echo $doc->saveHTML();

Output

<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate chip cookies">chocolate</a>
<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate chip cookies">chocolate</a>
<a class="dessert" href="http://mywebsite.com/chocolate.php" title="Try out our new chocolate chip cookies">chocolate</a>