Tuesday 27 February 2024

U-WPP-703 (Unit III: Chapter VI: String matching with regular expression )

 Syllabus: Unit III: Chapter VI: String matching with regular expression

6.1 What is regular expression

6.2 Pattern matching in Php

6.3 Replacing text

6.4 Splitting a string with a Regular Expression

6.1 What is regular expression:

Regular expressions are powerful pattern matching algorithm that can be performed in a single expression.

Regular expressions use arithmetic operators such as (+,-,^) to create complex expressions.

Regular expressions help you accomplish tasks such as validating email addresses, IP address etc.

Use of regular expressions:

 Regular expressions simplify identifying patterns in string data by calling a single function. This

saves us coding time.

 When validating user input such as email address, domain names, telephone numbers, IP

addresses, Highlighting keywords in search results

 When creating a custom HTML template. Regular expressions can be used to identify the template

tags and replace them with actual data.

PHP has built in functions that allow us to work with regular functions. Let’s now look at the commonly used

regular expression functions in PHP.

preg_match : this function is used to perform a pattern match on a string. It returns true if a match is found and

false if a match is not found.

preg_split : this function is used to perform a pattern match on a string and then split the results into a numeric

array

preg_replace : this function is used to perform a pattern match on a string and then replace the match with the

specified text.

Following is the syntax for a regular expression function such as preg_match,preg_split or preg_replace.

<?php

function_name('/pattern/',subject);

?>

 "function_name(...)" is either preg_match, preg_split or preg_replace.

 "/.../" The forward slashes denote the beginning and end of our regular expression

 "'/pattern/'" is the pattern that we need to matched

 "subject" is the text string to be matched against

6.2 Pattern matching in Php:

PHP Preg_match:

The first example uses the preg_match function to perform a simple pattern match for the word latur in a given

URL.

The code below shows the implementation for the above example.

<?php

$my_url = "www.shahucollegelatur.org.in";

if (preg_match("/latur/", $my_url))

{

echo "the url $my_url contains latur";

}

else

{

echo "the url $my_url does not contain latur";

}

?>

6.3 Replacing text:

PHP Preg_replace:

Let’s now look at the preg_replace function that performs a pattern match and then replaces the pattern with

something else.

The code in following example searches for the word do in a string.

It replaces the word do with the word end.

<?php

$text = "Politics always change. Stories never do.";

$text = preg_replace("/do/", 'end', $text);

echo $text;

?>

6.4 Splitting a string with a Regular Expression

PHP Preg_split:

We will take a string phrase and explode it into an array; the pattern to be matched is a single space.

The text string to be used in this example is " This is my college ".

Example.

<?php

$my_text="This is my college";

$my_array = preg_split("/ /", $my_text);

print_r($my_array );

?>

No comments:

Post a Comment