detect plain text or html
-
14-11-2019 - |
Question
In my application I am processing mail texts using PHP. For some mail clients such as sina.com is sending bad headers. For example, they are sending HTML mail but sending the header Content-Type as text/plain.
Now, for these mails, my application is considering the HTML as text. In PHP how can I detect if a text contain html text or not?
Solution
Yes, you can use strip_tags()
and compare filtered mail-body with original one to see are there any differences but don't forget, in plain-text you may have HTML tags as normal text and I think that strip_tags()
will remove these tags too.
OTHER TIPS
preg_match('/\<html\>(.*)\<\/html\>/', $emailbody)
If this match, then it is HTML.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow