php - How to detect HTML tags in text? -



php - How to detect HTML tags in text? -

i want see if $text value contains html formatting, can adjust phpmailer email accordingly either html or plain text. text looses line feeds, if seen html.

$email_body="<p><b>this html code</b></p>"; $text = htmlentities($email_body); if (strpos($text,"<b>") !== false) { echo " found b "; }

this not find this.

there ways using regex via preg_match or domdocument there easier way: utilize strip_tags , compare stripped value non-stripped value.

// set test array of mixed strings. $email_body_array = array(); $email_body_array[] = "<p><b>this html code.</b></p>"; $email_body_array[] = "this plain text."; $email_body_array[] = "this <b>bold</b> statement.."; // roll through strings & test them. foreach ($email_body_array $email_body) { $email_body_stripped = strip_tags($email_body); $text = htmlentities($email_body); if ($email_body !== $email_body_stripped) { echo "that has html!<br />"; } else { echo "boring, plain old text.<br />"; } }

this output of above showing concept in action:

that has html!

boring, plain old text.

that has html!

php html strpos

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -