Regular expressions are patterns used to match strings. They allow for complex pattern matching using operators like +, -, and ^. Regular expressions are useful for validating user input, highlighting keywords, and parsing HTML templates by identifying tags. The preg_match, preg_split, and preg_replace functions in PHP allow using regular expressions to search, split, and replace patterns in strings.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
35 views6 pages
String Matching With Regular Expression
Regular expressions are patterns used to match strings. They allow for complex pattern matching using operators like +, -, and ^. Regular expressions are useful for validating user input, highlighting keywords, and parsing HTML templates by identifying tags. The preg_match, preg_split, and preg_replace functions in PHP allow using regular expressions to search, split, and replace patterns in strings.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6
String matching with regular expression
ETL LABS PVT LTD – PHP 99
What is a Regular Expressions? Regular expressions are powerful pattern matching algorithm that can be performed in a single expression. 1
Regular expressions use arithmetic
operators such as (+,-,^) to create complex expressions.
ETL LABS PVT LTD – PHP 100
Why to use regular expressions Why to use regular expressions • Regular expressions simplify identifying patterns in string data by calling a single function. This saves us coding time. 2 • 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.
ETL LABS PVT LTD – PHP 101
<?php preg_match $my_url = "www.etllearninghub.com"; this function is used to perform a pattern if (preg_match("/guru/", $my_url)) match on a string. It returns true if a match { echo "the url $my_url is found and false if a match is not found. 3 contains guru"; } The first example uses the preg_match else function to perform a simple pattern match { for the word guru in a given URL. echo "the url $my_url does not contain guru"; } ?>
ETL LABS PVT LTD – PHP 102
preg_split this function is used to perform a pattern match on a string and then split the results into a numeric array
4 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
"I Love Regular Expressions".
ETL LABS PVT LTD – PHP 103
preg_replace this function is used to perform a pattern <?php match on a string and then replace the match with the specified text. $text = "We at ETL LABS strive to make quality education affordable to Let’s now look at the preg_replace the masses. etllearninghub.com"; function that performs a pattern match and 5 then replaces the pattern with something $text = preg_replace("/Guru/", '<span else. style="background:yellow">Guru</spa The code below searches for the word guru n>', $text); in a string. It replaces the word guru with the word guru echo $text; surrounded by css code that highlights the background colour. ?>