Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Strip Wrapping Paragraph Tags

I need to write a PHP function which removes opening and closing paragraph tags from a string, but only if they are at the very beginning/end. So the strings:

"Simple Test"
"<p>Here</p>"
"<p>Test <p>Nested</p> Outside </p>"

Would Output:

"Simple Test"
"Here"
"Test <p>Nested</p> Outside"

Can HTMLPurifier do this or should I use substr? My first attempt was:

if(strpos($str,'<p>') === 0  && strcmp(substr($str,-1,4),'</p>'))
$str = substr($str,3,strlen($str)-4);
like image 380
pws5068 Avatar asked Nov 30 '22 09:11

pws5068


2 Answers

This is a regex solution:

$str = preg_replace('!^<p>(.*?)</p>$!i', '$1', $str);
like image 60
thirtydot Avatar answered Dec 06 '22 14:12

thirtydot


this is a regex way.

its fine if the only requirement is to strip the exact wrapping strings <p> and </p>

if you need a generic solution which is robust for html you should use DOM. (for example if you want to acceppt classes, ids and variaous attributes in your wrapping paragraph tags.) but be aware that loading a domdocument will normalize your html.

<?
$str = array(
"Simple Test",
"<p>Here</p>",
"<p>Test <p>Nested</p> Outside </p>"
);

foreach($str as $st) {
  echo $st." ---> ";
  if(preg_match('#<p>(.+)</p>#',$st,$match) === 1) { // 1 if matched, 0 if not matched
    $st = $match[1]; // if matched, replace our string by the match
  }
  echo $st."\n";
}

this will generate this output:

Simple Test ---> Simple Test
<p>Here</p> ---> Here
<p>Test <p>Nested</p> Outside </p> ---> Test <p>Nested</p> Outside 

you could easily make thie a one liner. for example with preg_replace and regex backreferences you could replace the string which the match... but i hope in this form its more understandable to you.

like image 27
The Surrican Avatar answered Dec 06 '22 15:12

The Surrican