PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to remove paragraph tag <p> with contains special word in preg_replace?

 November 20, 2022     html, paragraph, php, preg-replace     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing