How can I check if PHP string contents contain any HTML contents?
I'm not good with Regular Expressions so I would like to have a function named "is_html
" to check this. :) thank you!
How can I check if PHP string contents contain any HTML contents?
I'm not good with Regular Expressions so I would like to have a function named "is_html
" to check this. :) thank you!
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.)
This looks for any string containing /> with zero or more letters between the slash and closing bracket.
The above function returns:
Here's what I came up with
You just pass a string and check if it returns true or false. As simple as that.
probably the easiest way would be something like:
Instead of using regex (like the other suggestions here) I use the following method:
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.
That depends on what you define to be html contents.
The most straightforward thing is to test if the string contains the html tag which can be done with the regex
In php the test will be
If you want to see you have valid html it's better to use a html parser.
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 :Edit: You should give a look at Kevin Traas answer just below. his regex will probably return less false positive.