Issue
For example i have this following code :
$string = '<p>i need to go to my room</p><p>i don't need to go to school</p><p>i need to study everywhere</p>';
Now, I want to remove the paragraph that contain words 'to school'.
The output I want is <p>
i need to go to my room </p><p>
i need to study everywhere </p>
How do i do it with preg_replace ?
Solution
Note that it is generally evil and bad practice to use regex to parse HTML. But, assuming you only have a string with a set of top level HTML <p>
tags, regex might be an option here.
$string = "<p>i need to go to my room</p><p>i don't need to go to school</p><p>i need to study everywhere</p>";
$output = preg_replace("/<p>((?!<\/p>).)*\bto school\b.*?<\/p>/", "2000", $string);
echo $output;
This outputs:
<p>i need to go to my room</p>2000<p>i need to study everywhere</p>
Here is an explanation of the regex:
<p> match an initial <p> tag
((?!<\/p>).)* match any character, provided that we do not
encounter a closing </p> tag
\bto school\b match the literal text "to school"
.*?<\/p> consume the remainder until the first closing </p>
The only perhaps tricky part of the above regex is:
((?!<\/p>).)*
This is a tempered dot, and it works by applying a negative lookahead (?!</p>)
at each step we match any character. This is critical, because it ensures that the regex engine will not accidentally cross over multiple <p>
tags to find to school
.
Answered By - Tim Biegeleisen Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.