ADVERTISEMENT
ADVERTISEMENT

PHP program to check a case insensitive substring exists within the given string using regular expression pattern

Here, we are going to learn how to check a case insensitive substring exists within the given string using regular expression pattern in PHP?
Submitted by Nidhi, on November 23, 2020

In this program, we will perform regular expression pattern matching using the preg_match() function, here we checked a case insensitive substring exists in the specified string or not.

Program/Source Code:

The source code to check a case insensitive substring exists within the given string using a regular expression pattern is given below. The given program is compiled and executed successfully.

<?php
//PHP program to check a case in-sensitive 
//substring is exists within the given string 
//using regular expression pattern.
$str = "www.includehelp.com";
$pattern = "/IncludeHelp/i";

$res = preg_match($pattern, $str);

if ($res == 1)
{
    printf("Substring found successfully");
}
else
{
    printf("Substring did not find in the string");
}

?>

Output:

Substring found successfully

Explanation:

Here, we created a string variable $str, which is initialized with "www.includehelp.com". After that, we used 'i' in the pattern to search case insensitive substring in the specified string. Here we used the preg_match() function that will return 1 on a successful match, otherwise, it will return 0. Then we used the if condition to print the appropriate message on the webpage.

PHP Regular Expressions Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.