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

Chapter 2-3 PHP Working With Strings

This document discusses working with strings in PHP. It covers searching text within strings, replacing text, uppercase and lowercase formatting, string functions, escape sequences, accessing characters, and regular expressions. Functions for string operations include strlen(), substr(), strstr(), strpos(), and strpbrk(). Heredoc and nowdoc syntax allow defining multi-line strings with custom delimiters.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Chapter 2-3 PHP Working With Strings

This document discusses working with strings in PHP. It covers searching text within strings, replacing text, uppercase and lowercase formatting, string functions, escape sequences, accessing characters, and regular expressions. Functions for string operations include strlen(), substr(), strstr(), strpos(), and strpbrk(). Heredoc and nowdoc syntax allow defining multi-line strings with custom delimiters.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

PHP

PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 1

Slide 1

CHAPTER 2:
PHP Working with Strings (3 of 5)
Topics covered:-
Searching text within a string
Replace some text within a string with another string
Working with Uppercase and Lowercase
String formatting
Regular Expressions

A full list of PHP string functions can be found in :-


www.php.net/manual/en/ref.strings.php

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 2

Escape Sequence Slide 2

• Use inside double-quotation.


Sequence Meaning
\n Newline / line feed character.
\r Carriage return
\t Horizontal tab character
\v Vertical tab character
\f Form feed character
\\ Escape backslash
\$ Escape dollar sign
\” Escape double quote

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 3

Creating and Accessing Strings Slide 3

• Differences using Single and Double quote ‘ ’ “ ” to


enclose a string:
$string1 = "hello"; //using double quote
$string2 = 'hello'; //using single quote

echo "$string1 . Welcome my friend.<br>";


echo '$string2 . Welcome my friend.<br>';
echo "<pre>$string1 there\\\"How are you?</pre>";
echo '<pre>$string2 \tthere\\\'\nHow are you?</pre>';

Note: In single quote, both variables and escape sequence will


not be evaluated. Refer to code example: stringDemo
AMIT 2043 Web Systems and Technologies
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 4

Complex String Expression Slide 4


Example 1:
$myFavoriteAnimal = "cat";
echo "My favorite animal is $myFavoriteAnimals";
• We would expect that PHP engine translate the above echo and the
browser will get the below printed:-
My favorite animal is cats
• Unfortunately, this is the result:-
My favorite animal is
• Solution: using curly braces
echo "My favorite animal is {$myFavoriteAnimal}s";
Example 2:
$myArray["age"] = 20;
echo "My age is {$myArray["age"]}";

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 5

Using Own Delimiters Slide 5

• Imagine a situation where you need to write a long


string with a lot of single-quote and double-quote
escape sequence.
• In order to avoid the above trouble, use heredoc and
nowdoc syntax.
• heredoc works like double quotation which replace
variable name with variable values and can include
escape sequence.
• nowdoc works like single quotation. Could not replace
variable name with variable values and cannot include
the use of escape sequence.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 6

Syntax for heredoc and nowdoc Slide 6

heredoc syntax:-
$myString = <<<DELIMITER
(insert text here)
DELIMITER;

nowdoc syntax:-
The difference between
$myString = <<<‘DELIMITER’
heredoc and nowdoc is with
(insert text here) the single-quote enclosing
DELIMITER; the DELIMITER

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 7

Example of heredoc Slide 7

heredoc example:-
$religion = ‘Hebrew’;

$myString = <<<DELIMITER
“’I am a $religion,’ he cries – and then – ‘I fear the Lord the God of Heaven
who hath made the sea and the dry land!’”
DELIMITER;

echo “<pre>$myString</pre>”;

Resulted Output:-
"'I am a Hebrew,' he cries - and then - 'I fear the Lord the God of Heaven
who hath made the sea and the dry land!'"
AMIT 2043 Web Systems and Technologies
Refer to code example: stringDemo
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 8

Example of nowdoc Slide 8

nowdoc example:-
$religion = ‘Hebrew’;

$myString = <<<‘DELIMITER’
“’I am a $religion,’ he cries – and then – ‘I fear the Lord the God of Heaven
who hath made the sea and the dry land!’”
DELIMITER;

echo “<pre>$myString</pre>”;

Resulted Output:-
"'I am a $religion,' he cries - and then - 'I fear the Lord the God of Heaven who
hath made the sea and the dry land!'"

AMIT 2043 Web Systems and Technologies


Refer to code example: stringDemo
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 9

Find the Length of a String Slide 9


• strlen() function takes in a string and return the number of characters in the
string.
• Example 1:-
echo strlen(“Hello World!”); //returns 12 count include whitespace
• This function is often useful to loop through all the characters in a string or
to validate a string to make sure it is in the correct length.

• Example 2:-
$studID = '13WAD01234';
if(strlen($studID) != 10){
echo "Invalid student id.";
}else{
echo "Valid student id.";
}

AMIT 2043 Web Systems and Technologies


Refer to code example: stringDemo
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 10

Accessing Characters Within a String Slide 10


• You can access a single character in a string by using [index]/[position] of that
string.
• If you wish to access certain part of a string, use substr() function.
• Example 1, access a single character

$string1 = “Hello World!”;


echo $string1[6]; //Displays ‘W’

• Example 2, access certain part of a string


echo substr($string1, 0, 5); //Display “Hello” coz retrieve substring
of $string1 starting from index 0 and
count for 5 characters long.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 11

Accessing Characters Within a String Slide 11

• Example, access certain part of a string


$string1 = “I can't change the direction of the wind,
but I can adjust my sails to always reach my
destination. Jimmy Dean”;

echo substr($string1, -10); //returns Jimmy Dean

• Noticed that in the above example, negative values are used. If negative
values are supplied, substr() function will count from end of the string
starting from -1, -2 and so on.
• Thus, -10 in the above example means reading start from the last index and
count for 10 characters long.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 12

Searching Strings Slide 12


• Sometimes is good to know whether a search text is within another
string.

• PHP offers several string function for that purpose:-


• strstr()
• strpos() and strrpos()
• substr_count()
• strpbrk()

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 13

Search with strstr() function Slide 13


• strstr() tells you whether the search text is within the string. If yes, returns
starting the search text until the end of the string otherwise returns false.
• Syntax:-
strstr(<string to search through>, <search text>)
• Example:
$myString = “Hello, World!”;
echo strstr($myString, “World”); //Displays “World!”
echo (strstr($myString, “boy”) ? “Yes” : “No”); //Display “No”
• From PHP 5.3, an additional argument false is supported. If true is supplied as
the third argument, strstr() function will return from the beginning of the
string to search through until the text is found.
echo strstr($myString, “World”, true); //Display “Hello, ”

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 14

Locate Text with strpos() or strrpos() Slide 14

• strpos() function returns the index of the first character of the found
text. If not found, strpos() return false.

• Syntax:-
strpos(<string to search through>, <search text>)
Note: there is an optional third argument to specify the
index to start the search.

• Example:-
echo strpos(“Hello My Friend”, “My”); //return 6
echo strpos(“Hello My Friend”, “hi”); //return false
echo strpos(“Hello World”, “o”, 5); //return 7

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 15

Find All Occurrence of the Search Text Slide 15


• Basically, you can use the strpos() function repeatedly with the third argument
to find all occurrence of the search text.
• Example:-
$myString1 = "Hello World, my friend, lol";
while (($pos = strpos($myString1, “l”, $pos)) != false){
echo "The letter {$find} is found in position: {$pos}<br>";
$pos++;
}
• Resulted output:-

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 16

strrpos() to Find the Last Match Slide 16

Example: echo strrpos("Hello World, my friend, lol", "o"); //Display 25

substr_count()
Example: echo substr_count("Hello ld World, my friend, lol", "ld"); //Display 2

Note: An optional third argument to indicate the index position to start


searching.
An optional fourth argument to specify how many characters the function
should search before giving up.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 17

Replacing Text Slide 17

• str_replace() – replacing all occurrences of the search text within the


target string

• substr_replace() – replaces a specified portion of the target string with


another string

• strtr() – replaces certain characters in the target string with other


characters

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 18

Replacing All Occurrences using str_replace() Slide 18


• str_replace() – replacing all occurrences of the search text within the
target string
• Syntax:-
str_replace(<search string>,
<replacement string>,
<string to search through>)

• Note: An optional forth argument that allows the programmer to specify


a variable. And when the replace completed, the variable will hold the
number of times the search text is replaced.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 19

Replacing All Occurrences using str_replace() Slide 19

• Example:-
echo str_replace("times", "bananas", "It was the best of times, it was
the worst of times“, $occurrence);
echo $occurrence;

Resulted output:-
It was the best of bananas, it was the worst of bananas2

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 20

Replace a Portion of a String Using Slide 20

substr_replace() Function
• substr_replace() – replaces a specified portion of the target string with
another string
• Syntax:-
substr_replace(<string>,
<replacement string>,
<start index>)
• With the start index, the replacement string start filling from the index
specified and onwards changing the original string.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 21

Replace a Portion of a String Using Slide 21

substr_replace() Function
• Example:-
$myString = “It was the best times, it was the worst times.”;
echo substr_replace($myString, "bananas", 11);

Resulted Output:-
It was the bananas

Noticed that the rest of the strings are all been replaced with bananas

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 22

Replace a Portion of a String Using Slide 22

substr_replace() Function
• If you wish not to replace the rest of the string with the replacement
string and would like to retain them, use the fourth argument to specify
the number of characters to replace.
• Example:-
$myString = “It was the best times, it was the worst times.”;
echo substr_replace($myString, "bananas", 16, 5);

Resulted Output:-
It was the best bananas, it was the worst times.

• Noticed that only the 5 characters “times” starting from index 16 has
been replaced with the replacement string. The rest of the string
remained unchanged.
AMIT 2043 Web Systems and Technologies
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 23

Replace a Portion of a String Using Slide 23

substr_replace() Function
• strtr() – replaces certain characters in the target string with other
characters
• Often in web programming, a URL should be made friendly by replacing
all the spaces with ‘+’ (plus) and any apostrophes with ‘-’ (hypen)
symbols.
• Example:-
$urlString = “Here’s is a little string”;
strtr($urlString, “ ‘”, “+-”);

• Resulted Output:-
Here-s+is+a+little+string

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 24

Working with Uppercase/Lowercase Slide 24

Uppercase / Description
Lowercase Function
strtolower() Convert a string to all lowercase.

strtoupper() Convert a string to all uppercase.

ucfirst() Convert the first character to uppercase.

lcfirst() Convert the first character to lowercase.

ucwords() Convert the first character of each word to


uppercase.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 25

Working with Uppercase/Lowercase Slide 25


• Example:-
$testCase = "opera mini opera mobile dolphin chrome";
echo ucwords($testCase) . "<br>";
echo lcfirst($testCase) . "<br>";
echo ucfirst($testCase) . "<br>";
echo strtolower($testCase) . "<br>";
echo strtoupper($testCase) . "<br>";

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 26

Case Sensitive Slide 26


• Most of the search and replacement functions discussed throughout the
text are case sensitive. Meaning to say, they match characters of the
same case.
• Example:-
$myString = “Hello World”;
if(strstr($myString, “hello”))
echo “Found”;
else
echo “Not Found”; //Display Not Found

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 27

Case Sensitive Slide 27


• Fortunately, PHP does includes case-insensitive versions of
many string functions:-
Function Case-Insensitive Equivalent

strstr() stristr()

strpos() stripos()

strrpos() strripos()
• Example:- str_replace() str_ireplace()
$myString = “Hello World”;
if(stristr($myString, “hello”))
echo “Found”;
else
echo “Not Found”; //Display Found

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 28

Formatting String using printf() & sprintf() Slide 28


functions
• Recall printf() used quite extensively in your C programming course
earlier.
• It works the same way in PHP which printf() requires you to specify the
conversion specification.
• Example of conversion specification are as follows:-
• %d
• %f
• %s
• %b
• %c
• %o and etc.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 29

Type Specifiers Slide 29


Type Specifier Meaning

b Treat the argument as an integer and format it as a binary number.

c Treat the argument as an integer and format it as a character with that ASCII value.

d Treat the argument as an integer and format it as a signed decimal number.

e Format the argument in scientific notation (for example, 3.45e+2)

f Format the argument as a floating-point number, taking into account the current locale settings (for
example, many European locales use a comma for the decimal point, rather than a period.)

F Format the argument as a floating-point number, ignore the locale.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 30

Type Specifiers (Continue) Slide 30


Type Specifier Meaning

o Treat the argument as an integer and format it as an octal number.

s Format the argument as string.

u Treat the argument as an integer and format it as a unsigned decimal number.

x Treat the argument as an integer and format it as a lowercase hexadecimal number.

X Treat the argument as an integer and format it as a uppercase hexadecimal number.

% Display a literal percent (%) symbol. This doesn’t require an argument.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 31

Specifying Sign Slide 31


• By default, printf() displays negative numbers with a minus ( - ) symbol.
In order to print ( + ) sign, we need to add a sign specifier in the type
specifier.

• Example:-
printf("%d", 123); //display 123
printf("%d", -123); //display -123
printf("%-d", -123); //display -123
printf("%+d", 123); //display +123
printf("%+d", -123); //display -123

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 32

Padding the Output Slide 32


• Using padding specifier.
• The purpose is to format output with leading zeros to a number or
padding zeros for alignment.
• Padding specifier consists of the following two (2) :-
• Zero (to pad with zeros)
• Space (to pad with spaces)
• Example:-
• You would like to ensure the number printed is always 8 digits long.
printf("%08d", 123); //display 00000123

Note that padding does not truncate the number output, for example,
printf("%06d", 12345678); //display 12345678
However, this is not the case with string padding.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 33

Padding the Output (Right-aligned) Slide 33


• Example:-
• You would like to ensure the text printed is always aligned right.
echo "<pre>";
printf("%20s\n", "Welcome");
printf("%20s\n", "Android User");
printf("%20s\n", "in Chrome");
printf("%20s\n", "Best for All Browser");
echo "</pre>";

Note : You may put a ( - ) minus symbol in front of the number 20 to make it left-
aligned instead of the default right-aligned.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 34

Padding the Output (Left-aligned) Slide 34


• Example:-
• You would like to ensure the text printed is always aligned left.
echo "<pre>";
printf("%-20s\n", "Welcome");
printf("%-20s\n", "Android User");
printf("%-20s\n", "in Chrome");
printf("%-20s\n", "Best for All Browser");
echo "</pre>";

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 35

Using own padding character Slide 35


• Yes, you may use your own padding character by specifying an
apostrophe before using the new padding character.

• Example:-
printf("%'*-50s\n", "Own padding character using (') ");

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 36

Formatting Floating-point Numbers Slide 36


• Formatting the number of decimal places using precision specifier.
• To use precision specifier, use a . (dot) followed by the number of
decimal places to use.
• Example:-
echo "<pre>";
printf("%f\n", 12);
printf("%.3f\n", 12);
printf("%12.3f\n", 12); Default precision
printf("%010.3f\n", 12);
echo "</pre>";

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 37

String Padding Slide 37

• Example:-

printf("%.10s", "Too good to be true");

• Resulted Output:-
Too good t

• As mentioned, string padding with the padding specifier used will


truncate the output to the exact number of characters. This lead to
character string losses. Be careful.

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 38

Storing Result of printf() Using sprintf() Slide 38


• There are cases where you would not want to always print the result
generated by printf() function but to keep it for later use.
• With this, sprintf() is used to keep the resulted value in a variable.

• Example:-
$welcomeMsg = sprintf(“%.7s”, “Welcome to PHP World”);
echo $welcomeMsg; //display Welcome

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 39

Trimming strings Slide 39


• trim() – removes white space at the beginning and the end of the string
– trim all white spaces
• ltrim() – left trim – removes white spaces only from the beginning of a
string
• rtrim() – right trim removes white spaces only from the end of the string
• Trimming string functions are always useful given that your program only
accept a string with 10 characters length. If the data with too many
white spaces, the string read will loss its meaning.

• Note: You may specify the second argument for the type of character to
trim.

Refer to example code stringDemo


AMIT 2043 Web Systems and Technologies
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 40

printf() vs str_pad() Slide 40


• Syntax:
str_pad(<string to pad with>,
<number of length returned>,
<symbol to pad: default is space>,
<alignment constant>);

Note: Alignment constants include:-


STR_PAD_LEFT, …RIGHT or …BOTH
• Example:
str_pad(“Benjamin Franklin”, 26, “&”, STR_PAD_BOTH);

• Resulted Output:-
&&&&Benjamin Franklin&&&&&

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 41

wordwrap() function Slide 41


• From the function name implied that wordwrap is a function similar to
that available in Ms. Word.
• If you have not used wordwrap in Ms. Word earlier, think of inserting a
table into a blank word document. In a particular table cell, you try to
add a long text. You should noticed that the text that you added will
automatically wrap into newline making it easier to read rather than a
long text with 500 characters.
• Similarly, wordwrap() function in PHP does that too.
• Wordwrap() function will split the long text into several lines using “\n”
character.
• It ensure that each line split is no longer than 75 characters.

Refer to example code stringDemo


AMIT 2043 Web Systems and Technologies
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 42
Formatting Numbers with number_format()
function Slide 42

• This function allows you to format the number with thousand separator
(,) and the specified number of decimal places.
• Of course, if you wish to use a different symbol for the thousand
separator, say (<space>), you can specify it into the number_format()
function as a second argument.
• Syntax:-
number_format(12345678, 2, “!”, “ ”);
• Example:- Number of decimal point Symbol for decimal
echo number_format(1234567823, 1, “!”, “@”);
Symbol for thousand
separator
• Resulted Output:-
1@234@567@823!0

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 43

Regular Expression Slide 43


• Regular Expressions are patterns that are used for matching and manipulating
strings according to specified rules
Example:
if(preg_match(‘ /(\d{6})-(\d{2})-(\d{4})/', $str)){

}
• PHP supports two types of regular expressions:
• POSIX Extended
• Perl Compatible Regular Expressions (PCRE)
• Regular expression patterns normally consist of a combination of:-
literal characters
Meta-characters
Quantifiers
Character classes

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 44

Meta-characters Slide 44
Meta-character Description
\ Escape character
^ beginning of a string
$ the end of a string
. Any single character except newline
| Alternatives (or)
[ Start of a character class
] End of a character class
( Start of a subpattern
) End of a subpattern
{ Start of a quantifier
} End of a quantifier

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 45

Quantifiers Slide 45

Character Meaning
? 0 or 1
* 0 or more
+ 1 or more
{x} Exactly x occurrences
{x, y} Between x and y (inclusive)
{x,} At least x occurrences

AMIT 2043 Web Systems and Technologies


PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 46

Character Classes Slide 46

Class Shortcut Meaning


[0-9] \d Any digit
[\f\r\t\n\v] \s Any white space

[A-Za-z0-9_] \w Any word character


[^0-9] \D Not a digit
[^\f\r\t\n\v] \S Not white space
[^A-Za-z0-9_] \W Not a word character

regular_expr.php
AMIT 2043 Web Systems and Technologies
PHP
PHPWorking
Workingwith
withStrings
Strings(Part
(3 of35)of 5) Slide 47

PCRE Functions Slide 47


Function Description
preg_match(pattern, string [, c Perform a regular expression match and will return
aptured]) 0 or 1, indicating whether or not a match is found. If
found, the last argument will contain the first
match.
preg_match_all(pattern, string Perform a global regular expression match and will
, matches [, order]) return the no. of matches made or FALSE if none
found. The function takes an optional ordering
constant
preg_replace( pattern, replace Perform a regular expression search and replace.
ment,  Limit is used for limiting the no. of replacements
subject [, limit])
preg_split(pattern, string Split string by a regular expression
[, limit [,flags]])

AMIT 2043 Web Systems and Technologies

You might also like