PHP Questions Home

Categories

Arrays
Files
Forms
Functions
Images
MySQL
Numbers
Others
Strings
Website


PHP Functions

PHP Functions


More PHP

Top Questions
Ask a Question

How do I check if a string contains HTML?


There are several ways that you can check whether a string contains HTML or not.

It also depends how robust you want your check to be.

A simple way would be to test for the presence of < or > in the string - this does not guarantee it is HTML but often works if you just want a cursory check.

Another method would be to call strip_tags on the code and then check how that length measures up against the code without that function being called, a little like this:

<?php
$actual_length 
strlen($isithtml);
$stripped_length strlen(strip_tags($isithtml));
if(
$actual_length != $stripped_length) {
  echo 
"The length has changed when we call strip tags, therefore the string does contain HTML"

else { echo 
"HTML? What HTML? None to be seen here!"; }
?>



More strings PHP Questions

How do I make the contents of a string lower case?
How do I calculate the metaphone of a string?
How do I print the " in an echo statement?
How do I remove space from the start of a PHP string?
How do you check if a variable is set or not in PHP?