PHP find output programs (Regular Expressions) | set 1

Find the output of PHP programs | Regular Expressions | Set 1: Enhance the knowledge of PHP Regular Expressions concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 25, 2021

Question 1:

<?php
    $string = "Includehelp is web platform for learning, please www.includehelp.com";
    $search_pattern = "/INCLUDE/";
    echo preg_match($search_pattern, $string);
?>

Output:

0

Explanation:

In the above program, we created a variable $string that contains a sentence, and we created a variable $search_pattern to search the specified pattern into the above string. The preg_match() function is used to check a specified substring is present in the string or not. The preg_match() function will return 0 because the specified pattern is not matched in the string.

Question 2:

<?php
    $string = "Includehelp is web platform for learning, please www.includehelp.com";
    $search_pattern = "/INCLUDE/i";
    echo preg_match($search_pattern, $string);
?>

Output:

1

Explanation:

In the above program, we created a variable $string that contains a sentence, and we created a variable $search_pattern to search the specified pattern into the above string. The preg_match() function is used to check a specified substring is present in the string or not. Here, we used 'i' in the search pattern to ignore case for searching a sub-string. The preg_match() function will return 1 because the specified pattern is matched in a string.

Question 3:

<?php
    $string = "Includehelp is web platform for learning, please www.includehelp.com";
    $search_pattern = "/INCLUDE/i";
    echo preg_match_all($search_pattern, $string);
?>

Output:

2

Explanation:

In the above program, we created a variable $string that contains a sentence, and we created a variable $search_pattern to search the specified pattern into the above string. The preg_match_all() function is used to return the count, how many times the specified pattern matched in the string.

In the above example, INCLUDE matched 2 times by ignoring the case. That's why preg_match_all() will return 2, that will be printed on the webpage.

Question 4:

<?php
    $string = "Includehelp is web platform for learning, please www.includehelp.com";
    $pattern = "/INCLUDE/i";
    echo preg_replace($string, $pattern, "duggu");
?>

Output:

PHP Warning:  preg_replace(): Delimiter must not be alphanumeric or backslash 
in /home/main.php on line 4

Explanation:

The above program will generate a warning, because we did not use correct syntax for preg_replace() function. The correct syntax of preg_replace() is given below:

preg_replace($pattern,"duggu",$string); 

The preg_replace() is used to replace a specified pattern by specified substring in the string. Here, "include" will be replaced by "duggu" in the string $string.

Question 5:

<?php
    $string = "Includehelp is a web platform for learning, please www.includehelp.com";
    $pattern = "/INCLUDE/i";
    preg_replace($pattern, "duggu", $string);
    echo $string;
?>

Output:

Includehelp is a web platform for learning, please www.includehelp.com

Explanation:

In the above program, we created a variable $string that contains a sentence, and we created a variable $pattern to replace the specified pattern by another substring into the above string. 

The preg_replace() is used to replace a specified pattern by specified substring in the string, and return the modified string. But it will not change replace in the original string $string. That's why it will print "Includehelp is a web platform for learning, please www.includehelp.com" on the webpage.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.