Detect HTML Tags in a String

Detect HTML tags in a string

A simple solution is:

if($string != strip_tags($string)) {
// contains HTML

The benefit of this over a regex is it's easier to understand, however I could not comment on the speed of execution of either solution.

Check if a string is html or not

A better regex to use to check if a string is HTML is:


For example:

/^/.test('') // true
/^/.test('foo bar baz') //true
/^/.test('<p>fizz buzz</p>') //true

In fact, it's so good, that it'll return true for every string passed to it, which is because every string is HTML. Seriously, even if it's poorly formatted or invalid, it's still HTML.

If what you're looking for is the presence of HTML elements, rather than simply any text content, you could use something along the lines of:


It won't help you parse the HTML in any way, but it will certainly flag the string as containing HTML elements.

Finding HTML tags in string

A general advice: Dont use regex to parse HTML It will get messy if the HTML changes..

Use DOMDocument instead:

$str = <<<EOF
<div>Do not match me</div>
<div type="special_type" src="bla"> match me</div>
<a>not me</a>
<div src="blaw" type="special_type" > match me too</div>

$doc = new DOMDocument();
$selector = new DOMXPath($doc);

$result = $selector->query('//div[@type="special_type"]');

// loop through all found items
foreach($result as $node) {
echo $node->getAttribute('src');

PHP - Detect string if contains only HTML tags or text with HTML tags

Yes it isn't the right function. You have to use a regex for that.

Here is a regex that checks if in one string, there's only html tags :


You can use it with the preg_match() function of PHP like that. This function returns true or false depending on if the regex is matched (if there are only HTML tags).

$is_only_html = preg_match("#^(<[^>]*>)+$#", $string_input_to_check);

Hope it helps.

Detect html tag on a string, get the value and remove values inside html tag in javascript

So, what you want to be doing is to convert your string into a jQuery object. You can do so like this -

var str = "Hello World <br><p>1</p><em>My First Javascript</em>";
var $holder = $('<div>');

Now we have your string encapsulated within another div element. Next we extract the value within the <p> element -

var value = $holder.find('p').text(); // 1

Now that we have that value we can place it into the hidden input field -


Now to remove all other elements from the original string - we'll use the container we created earlier for this -


Now we can take the textual contents of $holder with $holder.text() and it should be just -

Hello World

If you would like to fiddle with this,

you can do so here -

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;

Related Topics

Leave a reply