0% found this document useful (0 votes)
68 views

3.PHP Regular Expression

This document discusses PHP strings and regular expressions. It covers 4 ways to create strings in PHP: single-quote strings, double-quote strings, heredoc, and nowdoc. It also discusses various string functions like strlen(), str_replace(), and explode(). For regular expressions, it explains patterns, modifiers, metacharacters and quantifiers. It provides examples of using regular expression functions like preg_match(), preg_match_all(), and preg_replace() in PHP code.

Uploaded by

kashish7377
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
68 views

3.PHP Regular Expression

This document discusses PHP strings and regular expressions. It covers 4 ways to create strings in PHP: single-quote strings, double-quote strings, heredoc, and nowdoc. It also discusses various string functions like strlen(), str_replace(), and explode(). For regular expressions, it explains patterns, modifiers, metacharacters and quantifiers. It provides examples of using regular expression functions like preg_match(), preg_match_all(), and preg_replace() in PHP code.

Uploaded by

kashish7377
Copyright
© © All Rights Reserved
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/ 23

PHP

STRINGS
REGULAR EXPRESSION
PHP STRINGS

• Strings is a stream of characters


• 4 ways of creating a string
✓ Single-quote strings
✓ Double-quote strings
✓ Heredoc
✓ Nowdoc.
PHP STRINGS
✓ Single-quote strings
<?php
// single-quote strings
$site = ‘hello'; hello
echo $site;
?>

<?php
// single-quote strings
$site = ‘WT';
echo ‘welcome to $site’; single-quotes strings in PHP do not process special characters($)
?>
PHP STRINGS
✓ double-quote strings

<?php
$name = "WT";
echo "Course: $name <br>";
echo 'Course: $name';
?>
PHP STRINGS
✓ Heredoc An identifier is given after the heredoc (<<< ) operator, after
which any text can be written as a new line is started. To close
the syntax, the same identifier is given without any tab or space.

Parsing is done processes $variable and special character

<?php
$input = <<<testHeredoc
Strings are defined as an array of characters.
testHeredoc;
echo $input;
?>
PHP STRINGS
✓ Nowdoc

<?php
$input = <<<'testNowdoc'
Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.
testNowdoc;
echo $input;
?>
PHP STRINGS
✓ Heredoc ✓ Nowdoc

<?php <?php
$Message = 'Hello'; $Message = 'Hello';
$str = <<<example $str = <<<‘example’
$Message all !!! $Message all !!!
example; example;
echo $str; echo $str;
?> ?>
PHP STRING FUNCTIONS

• strlen()
• strrev()
• str_replace()
• strpos()
• trim() - remove whitespaces or strings from both sides of a string.
• explode() - string into an array
• strtolower()
• strtoupper()
• strwordcount()
• substr()
PHP REGULAR EXPRESSION
• A regular expression is a sequence of characters that forms a search pattern. When
you search for data in a text, you can use this search pattern to describe what you
are searching for.
• A regular expression can be a single character or a more complicated pattern.
• Regular expressions can be used to perform all types of text search and text
replacement operations.
PHP-SYNTAX
• In PHP, regular expressions are strings composed of delimiters,
a pattern, and optional modifiers.
$exp = "/php/i";
✓ / is the delimiter,
✓ php is the pattern that is being searched for, and
✓ i is a modifier that makes the search case-insensitive.

• The delimiter can be any character that is not a letter, number,


backslash, or space. The most common delimiter is the forward-
slash (/), but when your pattern contains forward slashes it is
convenient to choose other delimiters such as # or ~.
PHP-REGULAR EXPRESSIONS MODIFIERS
Modifiers can change how a search is performed.
PHP-REGULAR EXPRESSION PATTERNS
Brackets are used to find a range of characters
PHP-REGULAR EXPRESSION METACHARACTERS
Metacharacters are characters with a special meaning

\ Escape character
PHP-REGULAR EXPRESSION QUANTIFIERS
Quantifiers define quantities
PHP-REGULAR EXPRESSION GROUPING
You can use parentheses ( ) to apply quantifiers to entire patterns.
They also can be used to select parts of the pattern to be used as a match.

<?php
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo preg_match($pattern, $str);
?>
PHP-REGULAR EXPRESSION GROUPING
Regular Expression Matches
hello The string “hello”
^hello The string which starts with “hello”
hello$ The string which has “hello” at the end.
^hello$ The string where “hello” is alone on a string.
[abc] a, b, or c
[a-z] Any lowercase letter
[^A-Z] Any letter which is NOT an uppercase letter
(gif|png) Either “gif” or “png”
[a-z]+ One or more lowercase letters
^[a-zA-Z0-9]{1, }$ Any word with at least one number or one letter
([ax])([by]) ab, ay, xb, xy
[^A-Za-z0-9] Any symbol other than a letter or other than the number
([A-Z]{3}|[0-9]{5}) Matches three letters or five numbers
PHP-FUNCTIONS
PHP-FUNCTIONS(Example)

<?php
$str = "Learn PHP";
$pattern = "/php/i";
echo preg_match($pattern, $str);
?>
PHP-FUNCTIONS(Example)

<?php
$str = "The rain fall rate is less in chennai.";
$pattern = "/ra/i";
echo preg_match_all($pattern, $str);
?>
PHP-FUNCTIONS(Example)
<?php
$regex = "/<b>(.*)<\/b>/U";
$inputString = "Name: <b>John</b> Position:
<b>Developer</b>";
preg_match_all($regex, $inputString, $output);
echo $output[0][0]." <br> ".$output[0][1]."\n";
?>
PHP-FUNCTIONS(Example)
<?php
$regex = "([0-9]+)";
$original = "Completed graduation in 2004";
$replaceWith = "2002";
$original = preg_replace($regex, $replaceWith,
$original);
echo $original;
?>
PHP-FUNCTIONS(Example)
<?php
// Declare a string
$ip = "134.645.478.670";
$regex = "/\./";
// Use preg_split() function to convert a given string into an array

$output = preg_split ($regex, $ip);


echo "$output[0] <br>";
echo "$output[1] <br>";
echo "$output[2] <br>";
echo "$output[3] <br>";
?>
PHP-REGULAR EXPRESSION GROUPING

<?php
$my_email = "[email protected]";
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/", $my_email))
{
echo "$my_email is a valid email address";
}
else
{
echo "$my_email is NOT a valid email address";
}
?>

You might also like