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);
This is a regex solution:
$str = preg_replace('!^<p>(.*?)</p>$!i', '$1', $str);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With