1

I have content that is being outputted via Wordpress, and it wraps paragraph <p> tags around the content. I need to remove the content within the first <p></p> .. How can I do this?

For example, output is:

<p>Posted by Bob</p>
<p>This is the content here</p>
<p>This is the second paragraph</p>

I'd like to remove the line <p>Posted by Bob</p> and leave the rest.

Martin Ender
  • 43,427
  • 11
  • 90
  • 130
Josh
  • 101
  • 2
  • 9

2 Answers2

2

Generally, you should not parse HTML with regex.

In this case, a regex solution might work fine though:

$str = preg_replace('~<p>.*?</p>~s', '', $str, 1);

The important things are

  • the ? which makes the .* ungreedy (so that it stop as soon as possible and does not continue until the very last </p>
  • the modifier s so that . is able to match new-lines
  • the limit 1 so that only the first match is removed

If you only want to remove the contents of the tag (so if you want to keep an empty paragraph), simply use <p></p> as the replacement string.

However, as I said this is still not a perfect solution. Think about <p> tags in comments, or <p> tags that have attributes or even invalid HTML. PHP comes with a DOM parser. If you can use 3rd-party libraries there is also this one, which is quite convenient to use and makes the problem almost trivial:

require "simple_html_dom.php";

$html = new simple_html_dom($input);
$html->load($input);

$html->find('p', 0)->outertext = "";

$output = $html->save();

Equally if you just want to empty the tag, but keep it, replace outertext with innertext.

Community
  • 1
  • 1
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
0

If you have access to jQuery you could use the following:

$("p:first-child").text("Hello World!");

To include jQuery in WordPress add the following in your <head></head>:

<?php wp_enqueue_script("jquery"); ?>
<?php wp_head(); ?>

This includes jQuery in a safe way and prevents any conflicts with other instances of jQuery or other libraries.

AntonNiklasson
  • 1,719
  • 1
  • 15
  • 28