Hey guys I would like your help with with preg_match and replace function I have this:
$replacement="NEW TEXT";
<p data-edit="1">TEXT A</p>
<div data-edit="2">TEXT B</div>
And I need to find div or p or anything with data-edit attribute that equals to some number, in this case for example 1 and replace the text inside for $replacement.
2
Answers
Initiate by setting the HTML content and the text intended for replacement, identify the specific number within the ‘
data-edit
‘ attribute targeting, and employ a regular expression to isolate the complete HTML element. This process involves capturing both the opening and closing tags, particularly pinpointing elements with a ‘data-edit
‘ attribute corresponding to the target number. Then utilize ‘preg_replace_callback
‘ to invoke a callback function, crafting the replacement string and substituting the original element content with the newly defined text. Hope it helps.Regular expressions are not reliable tools for parsing HTML. It is best practice to leverage a legitimate HTML parser. In this case, I find a combination of DomDocument and XPath to be a very direct approach. Because your HTML doesn’t have a (expected) parent element, I’ll manually add a parent
div
tag and then removed it when stringifying the result.The XPath query says at any depth in the document (
//
), match any element (*
) with the attributedata-edit
([@data-edit]
). To target a specificdata-edit
value, write like//*[@data-edit=2]
.Code: (Demo)
Notice in my demo that the code perfectly differentiates between qualified/disqualified
data-edit
attributes.Regex has to go to greater effort to safeguard against fringe cases.
Here is a somewhat related answer with similar insights: preg_replace img src to data-src stack overflow (PHP)
More specifically about designating a
data-edit
number in the query, write as the following for2
. Demo