html - In PHP, how to remove wrapping paragraph, but only if it's the only paragraph -


how remove (in case, useless) wrapping paragraph in cases this:

<p>only paragraph</p> 

but keep string when there more 1 paragraph involved:

<p>paragraph 1</p> <p>paragraph 2</p> 

preg_replace trick in here if can handle regexps.. :/

here 1 way, 2 test cases, $a , $b

$a = '<p>only paragraph</p>';  $b = '<p>only paragraph</p>       <p>only paragraph</p>       <p>only paragraph</p>';  // change values $a , $b if( count( explode('<p>', $a) ) == 2 ){   $c = preg_replace('#</?p>#', '', $a); }  if( isset( $c)) {     var_dump( $c ); } // 'only paragraph' 

you might need add trim() first well.

does not cater malformed input eg

<p>para1 </p> para 2</p> 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -