How to check if string contents have any HTML in it?
If you want to test if a string contains a "<something>"
, (which is lazy but can work for you), you can try something like that :
function is_html($string)
{
return preg_match("/<[^<]+>/",$string,$m) != 0;
}
Instead of using regex (like the other suggestions here) I use the following method:
function isHtml($string)
{
if ( $string != strip_tags($string) )
{
return true; // Contains HTML
}
return false; // Does not contain HTML
}
Here I use a PHP function strip_tags to remove any HTML from the string. It then compares the strings and if they do not match HTML tags were present.
probably the easiest way would be something like:
<?php
function hasTags( $str )
{
return !(strcmp( $str, strip_tags($str ) ) == 0);
}
$str1 = '<p>something with <a href="/some/url">html</a> in.';
$str2 = 'a string.';
var_dump( hasTags( $str1 ) ); // true - has tags.
var_dump( hasTags( $str2 ) ); // false - no tags.
The accepted answer will consider a string containing <something> as HTML which, obviously, it is not.
I use the following, which may or may not be a better idea. (Comments appreciated.)
function isHTML( $str ) { return preg_match( "/\/[a-z]*>/i", $str ) != 0; }
This looks for any string containing /> with zero or more letters between the slash and closing bracket.
The above function returns:
<something> is NOT HTML
<b>foo</b> is HTML
<B>foo</B> is HTML
<b>foo<b> is NOT HTML
<input /> is HTML