Reputation: 1090
I always encounter regular expressions but I don't really try to understand and use them. But my current project is forcing me to use a regular expression so I need someone who can give me the correct regex to replace a simple string. Basically I'm replacing a small subset of longtext retrieved from a database. The longtext is just a paragraph(s) with text anchors in a form of:
<a href="example.com" title="blah3x">Example</a>
So the question is how do I replace the value of the title attribute? Please note that the text may contain two more anchor tags so I'd like to able to specifically target each of them.
EDIT: I'd like to use pure PHP on this. I think I know how to do this using js/jquery.
Upvotes: 0
Views: 145
Reputation: 14990
You could do this with the following regex
(<a\b[^>]*?\btitle=(['"]))(.*?)\2
(
start capture group 1<a\b
consume open angle bracket and an a
followed by a word break[^>]*?
consume all non close angle bracket characters up to... this forces the regex to stay inside the anchor tag\btitle=
consume a word break and title=
, the break helps do some additional checking(['"])
capture group 2, ensure the an open single or double quote is being used)
close capture group 1(.*?)
start capture group 3, and non greedy consume to collect all text inside the quotes\2
reference back to the string from capture group 2, if you used a single quote to open the value, then a single quote will be required to close the value. Same if you had use a double quote.In the replace command I'm simply replacing the entire found string from <a to the close quote
with: group capture 1, followed by the desired text NewValue
followed by the close quote from group capture 2.
<?php
$sourcestring="<a href="example.com" title="blah3x">Example</a>";
echo preg_replace('/(<a\b[^>]*?\btitle=([\'"]))(.*?)\2/im','\1NewValue\2',$sourcestring);
?>
$sourcestring after replacement:
<a href="example.com" title="NewValue">Example</a>
Since parsing text via a html parser is not the desired solution, I'll skip the usual soap box disclaimer about parsing html with Regex.
Upvotes: 4
Reputation: 14103
$string=preg_replace(
'@<a (.*)title="(.*)"([^>]*)>(.*)</a>@iU',
'<a $1title="'.$replacement.'"$3>$4</a>',
$string);
Note that the i
at the end of the expression makes it case insensitive, and the U
makes it ungreedy.
Upvotes: 1
Reputation: 219804
$doc = new DOMDocument();
$doc->loadHTML('<a href="example.com" title="blah3x">Example</a>');
$anchors = $doc->getElementsByTagName('a');
foreach ($anchors as $anchor)
{
$anchor->setAttribute('target', '__blank');
}
$html = $doc->saveHTML();
echo $html;
Upvotes: 4