Question

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);
Was it helpful?

Solution

This is a regex solution:

$str = preg_replace('!^<p>(.*?)</p>$!i', '$1', $str);

OTHER TIPS

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.

A Regular Expression like

</??p(?:\s+\w*)>

will match your <p\ >, </p> and <p somestuff> - use that regexp and replace matches with emtpy string or whatever you like.

HTH

PS: use the "ignore case" flag, just in case.

Edit: made the group a non-capturing one.

The not so fancy pattern, but works $inf = preg_replace('/<[\/]*?p.*?>/', '', $info);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top