Search code examples
phpstrip-tags

strip_tags php removes too much


There's a little trouble with a malformed string including html (see the '' at the beginning and the <'blabla) and the function strip_tags() from PHP.

I've this code:

$str = "To: ''[email protected]' <'[email protected]>\nSubject: Hello World\nDear Ladies <b>and</b> Gentlemen,";
echo strip_tags($str);

With the following outout:

To: ''[email protected]' 

My wanted/expected result is:

To: ''[email protected]' 
Subject: Hello World
Dear Ladies and Gentlemen,

Do you have any idea to get this?


Solution

  • If stip_tags() didn't work as you expect, try this one.

    $str = "To: ''[email protected]' <'[email protected]>\nSubject: Hello World\nDear Ladies <b>and</b> Gentlemen,";
    
    $val = preg_replace('/<[^>]+?>/', ' ', $str);
    

    Your $val contains string without html values

    enter image description here