PHP Check If String Contains HTML Tags Example Tutorial

Reading Time: 3 minutes
1,486 Views

Inside this article we will see the concept i.e PHP Check If String Contains HTML Tags Example. Article contains the classified information about checking HTML tags in string values. Usually we know HTML tags contain the pair of angular brackets. So, we will use the concept of PHP function preg_match() for this.

The preg_match() function returns whether a match was found in a string.

If you are looking for an article which gives you the understanding of checking a string contains HTML Tag or not then this article will help you a lot for this.

Learn More –

Let’s get started.

Application Programming

Create a file index.php inside your localhost path. We will consider two examples to understand the concept in deep.

Example #1 (String with HTML Tag)

Open index.php and write this code into it.

<?php

$myString = "Hi <h3>Online Web Tutor</h3>, You are awesome!!!";

if (isHTML($myString)) {

    echo "There is a HTML Tag in String";
} else {

    echo "No HTML Tag in String";
}

function isHtml($string)
{
    return preg_match("/<[^<]+>/", $string, $m) != 0;
}

Output

There is a HTML Tag in String

Example #2 (String with No HTML Tag)

Open index.php and write this code into it.

<?php

$myString = "Hi Online Web Tutor, You are awesome!!!";

if (isHTML($myString)) {

    echo "There is a HTML Tag in String";
} else {

    echo "No HTML Tag in String";
}

function isHtml($string)
{
    return preg_match("/<[^<]+>/", $string, $m) != 0;
}

Output

No HTML Tag in String

We hope this article helped you to learn PHP Check If String Contains HTML Tags Example Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more