- Where Developers Learn, Share, & Build Careers


I have content that is being output via Wordpress, and this article is and lt; P & gt; Wrap the tag around content around me & lt; P & gt; & Lt; / P & gt; Need to remove content in . How can I do this?

For example, the output is:

  & lt; P & gt; Posted by Bob & lt; / P & gt; & Lt; P & gt; This is the content here & lt; / P & gt; & Lt; P & gt; This is the second paragraph & lt; / P & gt;   

I line & lt; P & gt; Posted by Bob & lt; / P & gt; and want to leave the rest. In general,.

In this case, a regex solution can work fine though

$ str = preg_replace ('~ & lt; p & gt; *? & Lt; / p & gt; ~ s,' ', $ str, 1);

Important things are

  • which ? creates . * ungreedy (so that it stops as soon as possible and very last & lt; / p & gt;
  • Modifier s . is capable of matching new lines
  • range 1 which removes only the first match

    If you want only to remove the contents of the tag (if you want to keep an empty paragraph), just click

    Use.

    However, as It is still not the right solution. Tags or comments about tags, or p> tags or attributes or Invalid HTML also comes with PHP. If you can use third party libraries, it is quite convenient to use and makes this problem almost trivial:

      "Simple_html_dom.php" is required; $ html = new simple_html_dom ($ input); $ html- & gt; load ($ input); $ Html- & gt; search ('P', 0) - & gt; External text = ""; $ Output = $ html-> Save ();   

    Even if you want to empty the tag but want to keep it, then external text with internal text Change .

Comments

Popular posts from this blog

java - NullPointerException for a 2d Array -

python - Assemble mpeg file unable to play in mediaplayer -

asp.net - MVC [HandleError] HandleErrorAttribute called twice when using global logging -