String Manipulation and Regular Expressions
Regular Expressions (POSIX) Explanation:
BRACKETS [ ] Shows the range of search from the string [0-9] matches any decimal digit from 0 through 9. [a-z] matches any character from lowercase a through lowercase z. [A-Z] matches any character from uppercase A through uppercase Z. [A-Za-z] matches any character from uppercase A through lowercase z.
Quantifiers The frequency or position of bracketed character sequences and single characters can be denoted by a special character, with each special character having a specific connotation. The +, *, ?,{occurrence_range}, and $ flags all follow a character sequence:
•  p+ matches any string containing at least one p. •  p* matches any string containing zero or more p’s. •  p? matches any string containing zero or one p. •  p{2} matches any string containing a sequence of two p’s. •  p{2,3} matches any string containing a sequence of two or three p’s. •  p{2,} matches any string containing a sequence of at least two p’s. •  p$ matches any string with p at the end of it. Still other flags can precede and be inserted before and within a character sequence: •  ^p matches any string with p at the beginning of it. •  [^a-zA-Z] matches any string  not  containing any of the characters ranging from a through z and A through Z. •  p.p matches any string containing p, followed by any character, in turn followed by another p.
• ^.{2}$ matches any string containing  exactly  two characters. • <b>(.*)</b> matches any string enclosed within <b> and </b> (presumably HTML bold tags). • p(hp)* matches any string containing a p followed by zero or more instances of the sequence hp. Search a dolor Sign in a string ([\$])([0-9]+)
Predefined Character Ranges (Character Classes) •  [:alpha:]: Lowercase and uppercase alphabetical characters. This can also be specified as [A-Za-z]. •  [:alnum:]: Lowercase and uppercase alphabetical characters and numerical digits. This can also be specified as [A-Za-z0-9]. •  [:cntrl:]: Control characters such as a tab, escape, or backspace. •  [:digit:]: Numerical digits 0 through 9. This can also be specified as [0-9]. •  [:graph:]: Printable characters found in the range of ASCII 33 to 126. •  [:lower:]: Lowercase alphabetical characters. This can also be specified as [a-z]. •  [:punct:]: Punctuation characters, including ~ ` ! @ # $ % ^ & * ( ) - _ + = { } [ ] : ; ' < > , . ? and /. •  [:upper:]: Uppercase alphabetical characters. This can also be specified as [A-Z]. •  [:space:]: Whitespace characters, including the space, horizontal tab, vertical tab, new line, form feed, or carriage return. •  [:xdigit:]: Hexadecimal characters. This can also be specified as [a-fA-F0-9].
ereg()  executes a case-sensitive search of string for pattern, returning  TRUE if the pattern is found and FALSE otherwise. eregi()   Unlike ereg(), the search is case insensitive.This function can  be useful when checking the validity of strings, such as passwords. ereg_replace()  The ereg_replace() function operates much like ereg(), except that the functionality is extended to finding and replacing pattern with replacement instead of simply locating it. If no matches are found, the string will remain unchanged. Like ereg(), ereg_replace() is case sensitive. eregi_replace()   The eregi_replace() function operates exactly like ereg_replace(), except that the search for pattern in string is not case sensitive. PHP’s Regular Expression Functions (POSIX Extended)
split()   The split() function divides string into various elements, with the boundaries of each element based on the occurrence of pattern in string. The optional input parameter limit is used to specify the number of elements into which the string should be divided, starting from the left end of the string and working rightward. In cases where the pattern is an alphabetical character, split() is case sensitive. Here’s how you would use split() to break a string into pieces based on occurrences of horizontal tabs and newline characters: spliti()  The spliti() function operates exactly in the same manner as its sibling split(), except that it is case insensitive. sql_regcase()  The sql_regcase() function converts each character in string into a bracketed expression containing two characters. If the character is alphabetic, the bracket will contain both forms; otherwise, the original character will be left unchanged. This function is particularly useful when PHP is used in conjunction with products that support only case-sensitive regular expressions. Here’s how you would use sql_regcase() to convert a string:
Practical Examples <?php $username = &quot;jasoN&quot;; if (ereg(&quot;([^a-z])&quot;,$username)) echo &quot;Username must be all lowercase!&quot;; ?> ereg() boolean ereg (string  pattern , string  string  [, array  regs ])
<?php $url = &quot;http://www.apress.com&quot;; // break $url down into three distinct pieces: // &quot;http://www&quot;, &quot;apress&quot;, and &quot;com&quot; $parts = ereg(&quot;^(http://www)\.([[:alnum:]]+)\.([[:alnum:]]+)&quot;, $url, $regs); echo $regs[0]; // outputs the entire string &quot;http://www.apress.com&quot; echo &quot;<br>&quot;; echo $regs[1]; // outputs &quot;http://www&quot; echo &quot;<br>&quot;; echo $regs[2]; // outputs &quot;apress&quot; echo &quot;<br>&quot;; echo $regs[3]; // outputs &quot;com&quot; ?> This returns: http://www.apress.com http://www apress com
eregi() int eregi (string  pattern , string  string , [array  regs ]) <?php $pswd = &quot;jasongild&quot;; if (!eregi(&quot;^[a-zA-Z0-9]{8,10}$&quot;, $pswd)) echo &quot;The password must consist solely of alphanumeric characters, and must be 8-10 characters in length!&quot;; ?> In this example, the user must provide an alphanumeric password consisting of 8 to 10 characters, or else an error message is displayed.
ereg_replace() string ereg_replace (string  pattern , string  replacement , string  string ) <?php $text = &quot;This is a link to http://www.wjgilmore.com/.&quot;; echo ereg_replace(&quot;http://([a-zA-Z0-9./-]+)$&quot;, &quot;<a href=\&quot;\\0\&quot;>\\0</a>&quot;,$text); ?> This returns: href=&quot;http://www.wjgilmore.com/&quot;>http://www.wjgilmore.com</a>.
split() array split (string  pattern , string  string  [, int  limit ]) <?php $text = &quot;this is\tsome text that\nwe might like to parse.&quot;; print_r(split(&quot;[\n\t]&quot;,$text)); ?> Array ( [0] => this is [1] => some text that [2] => we might like to parse. )
sql_regcase() string sql_regcase (string  string ) <?php $version = &quot;php 4.0&quot;; print sql_regcase($version); ?> Output:  [Pp] [Hh] [Pp] 4.0
Regular Expression Syntax (Perl Style) Modifiers
Metacharacters •  \A: Matches only at the beginning of the string. •  \b: Matches a word boundary. •  \B: Matches anything but a word boundary. •  \d: Matches a digit character. This is the same as [0-9]. •  \D: Matches a nondigit character. •  \s: Matches a whitespace character. •  \S: Matches a nonwhitespace character. •  []: Encloses a character class. A list of useful character classes was provided in the previous section. •  (): Encloses a character grouping or defines a back reference. •  $: Matches the end of a line. •  ^: Matches the beginning of a line. •  .: Matches any character except for the newline.
•  \: Quotes the next metacharacter. •  \w: Matches any string containing solely underscore and alphanumeric characters. This is the same as [a-zA-Z0-9_]. •  \W: Matches a string, omitting the underscore and alphanumeric characters.
Let’s consider a few examples: /sa\b/ Because the word boundary is defined to be on the right side of the strings, this will match strings like pisa and lisa, but not sand. /\blinux\b/i This returns the first case-insensitive occurrence of the word linux. /sa\B/ The opposite of the word boundary metacharacter is \B, matching on anything but a word boundary. This will match strings like sand and Sally, but not Melissa. /\$\d+\g This returns all instances of strings matching a dollar sign followed by one or more digits.
PHP’s Regular Expression Functions (Perl Compatible) preg_grep() array preg_grep (string  pattern , array  input  [,  flags ]) The preg_grep() function searches all elements of the array input, returning an array consisting of all elements matching pattern. Consider an example that uses this function to search an array for foods beginning with  p : preg_match() int preg_match (string  pattern , string  string  [, array  matches ] [, int  flags  [, int  offset ]]]) The preg_match() function searches string for pattern, returning TRUE if it exists and FALSE otherwise. The optional input parameter pattern_array can contain various sections of the subpatterns contained in the search pattern, if applicable. Here’s an example that uses preg_match() to perform a case-sensitive search:
preg_match_all() int preg_match_all (string  pattern , string  string , array  pattern_array [, int  order ]) The preg_match_all() function matches all occurrences of pattern in string, assigning each occurrence to array pattern_array in the order you specify via the optional input parameter order. The order parameter accepts two values: preg_quote() string preg_quote(string  str  [, string  delimiter ]) The function preg_quote() inserts a backslash delimiter before every character of special significance to regular expression syntax. These special characters include: $ ^ * ( ) + = { } [ ] | \\ : < >. The optional parameter delimiter is used to specify what delimiter is used for the regular expression, causing it to also be escaped by a backslash.
The preg_replace() function operates identically to ereg_replace(), except that it uses a Perlbased regular expression syntax, replacing all occurrences of pattern with replacement, and returning the modified result. The optional input parameter limit specifies how many matches should take place. Failing to set limit or setting it to -1 will result in  the  replacement of all occurrences. preg_replace() mixed preg_replace (mixed  pattern , mixed  replacement , mixed  str  [, int  limit ]) preg_replace_callback() mixed preg_replace_callback(mixed  pattern , callback  callback , mixed  str  [, int  limit ]) Rather than handling the replacement procedure itself, reg_replace_callback() function delegates the string-replacement procedure to some other user-defined function. The pattern parameter determines what you’re looking for, while the str parameter defines the string you’re searching. The callback parameter defines the name of the function to be used for the replacement task. The optional parameter limit specifies how many matches should take place. Failing to set limit or setting it to -1 will result in the replacement of all occurrences. In the following example, a function named acronym() is passed into preg_replace_callback() and is used to insert the long form of various acronyms into the target string
preg_split() array preg_split (string  pattern , string  string  [, int  limit  [, int  flags ]]) The preg_split() function operates exactly like split(), except that pattern can also be defined in terms of a regular expression. If the optional input parameter limit is specified, only limit number of substrings are returned
Practical of Perl Expressions preg_grep() array preg_grep (string  pattern , array  input  [,  flags ]) <?php $foods = array(&quot;pasta&quot;, &quot;steak&quot;, &quot;fish&quot;, &quot;potatoes&quot;); $food = preg_grep(&quot;/^p/&quot;, $foods); print_r($food); ?> This returns: Array ( [0] => pasta [3] => potatoes )
preg_match() int preg_match (string  pattern , string  string  [, array  matches ] [, int  flags  [, int  offset ]]]) <?php $line = &quot;Vim is the greatest word processor ever created!&quot;; if (preg_match(&quot;/\bVim\b/i&quot;, $line, $match)) print &quot;Match found!&quot;; ?> For instance, this script will confirm a match if the word Vim or vim is located, but not simplevim, vims, or evim.
preg_match_all() int preg_match_all (string  pattern , string  string , array  pattern_array  [, int  order ]) <?php $userinfo = &quot;Name: <b>Zeev Suraski</b> <br> Title: <b>PHP Guru</b>&quot;; preg_match_all (&quot;/<b>(.*)<\/b>/U&quot;, $userinfo, $pat_array); print $pat_array[0][0].&quot; <br> &quot;.$pat_array[0][1].&quot;\n&quot;; ?> This returns: Zeev Suraski PHP Guru
preg_quote() string preg_quote(string  str  [, string  delimiter ]) <?php $text = &quot;Tickets for the bout are going for $500.&quot;; echo preg_quote($text); ?> This returns: Tickets for the bout are going for \$500\.
preg_replace() mixed preg_replace (mixed  pattern , mixed  replacement , mixed  str  [, int  limit ]) <?php $text = &quot;This is a link to http://www.wjgilmore.com/.&quot;; echo preg_replace(&quot;/http:\/\/(.*)\//&quot;, &quot;<a href=\&quot;\${0}\&quot;>\${0}</a>&quot;, $text); ?> This returns: This is a link to <a href=&quot;http://www.wjgilmore.com/&quot;>http://www.wjgilmore.com/</a>.
preg_replace_callback() mixed preg_replace_callback(mixed  pattern , callback  callback , mixed  str [, int  limit ]) <?php // This function will add the acronym long form // directly after any acronyms found in $matches function acronym($matches) { $acronyms = array( 'WWW' => 'World Wide Web', 'IRS' => 'Internal Revenue Service', 'PDF' => 'Portable Document Format'); if (isset($acronyms[$matches[1]])) return $matches[1] . &quot; (&quot; . $acronyms[$matches[1]] . &quot;)&quot;; else return $matches[1];  }
This returns: The IRS (Internal Revenue Service) offers tax forms in PDF (Portable Document Format) on the WWW (World Wide Web). // The target text $text = &quot;The <acronym>IRS</acronym> offers tax forms in <acronym>PDF</acronym> format on the <acronym>WWW</acronym>.&quot;; // Add the acronyms' long forms to the target text $newtext = preg_replace_callback(&quot;/<acronym>(.*)<\/acronym>/U&quot;, 'acronym', $text); print_r($newtext);?>
preg_split() array preg_split (string  pattern , string  string  [, int  limit  [, int  flags ]]) <?php $delimitedText = &quot;+Jason+++Gilmore+++++++++++Columbus+++OH&quot;; $fields = preg_split(&quot;/\+{1,}/&quot;, $delimitedText); foreach($fields as $field) echo $field.&quot;<br />&quot;; ?> This returns the following: Jason Gilmore Columbus OH
 
 
 
 
 
 
 
 
 

More Related Content

PPTX
Regular Expressions in PHP
PPT
PHP Regular Expressions
PPT
Regular Expressions 2007
PPTX
Regular expressions and php
PPT
Regular Expressions in PHP, MySQL by programmerblog.net
PPT
Regular Expressions grep and egrep
ODP
Regular Expression
PPT
Textpad and Regular Expressions
Regular Expressions in PHP
PHP Regular Expressions
Regular Expressions 2007
Regular expressions and php
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions grep and egrep
Regular Expression
Textpad and Regular Expressions

What's hot (20)

PPT
Adv. python regular expression by Rj
PPT
Regular Expression
PPT
Class 5 - PHP Strings
PPTX
String variable in php
KEY
Andrei's Regex Clinic
ODP
Regex Presentation
PPTX
Regular Expression
PPTX
Python advanced 2. regular expression in python
DOCX
Python - Regular Expressions
PPTX
11. using regular expressions with oracle database
PPT
Bioinformatica 06-10-2011-p2 introduction
PPT
Regular Expressions
PPT
The Power of Regular Expression: use in notepad++
PDF
Ruby cheat sheet
PPT
Introduction to regular expressions
PDF
Strings in Python
PPTX
Bioinformatics p2-p3-perl-regexes v2014
PDF
Python (regular expression)
PPTX
Regular Expressions 101 Introduction to Regular Expressions
Adv. python regular expression by Rj
Regular Expression
Class 5 - PHP Strings
String variable in php
Andrei's Regex Clinic
Regex Presentation
Regular Expression
Python advanced 2. regular expression in python
Python - Regular Expressions
11. using regular expressions with oracle database
Bioinformatica 06-10-2011-p2 introduction
Regular Expressions
The Power of Regular Expression: use in notepad++
Ruby cheat sheet
Introduction to regular expressions
Strings in Python
Bioinformatics p2-p3-perl-regexes v2014
Python (regular expression)
Regular Expressions 101 Introduction to Regular Expressions
Ad

Similar to Php String And Regular Expressions (20)

PPTX
Regex posix
PPTX
php string part 4
PPT
Php Chapter 4 Training
PDF
Basta mastering regex power
PPTX
Regular expressions in php programming language.pptx
PDF
Tutorial on Regular Expression in Perl (perldoc Perlretut)
PPTX
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
PPT
Regex Basics
PDF
Lecture 23
PPT
regex.ppt
PPT
Perl Presentation
PPTX
Php pattern matching
PDF
Working with text, Regular expressions
PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Strings,patterns and regular expressions in perl
PPT
Basic perl programming
PDF
perl-pocket
PDF
perl-pocket
PDF
perl-pocket
Regex posix
php string part 4
Php Chapter 4 Training
Basta mastering regex power
Regular expressions in php programming language.pptx
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Regex Basics
Lecture 23
regex.ppt
Perl Presentation
Php pattern matching
Working with text, Regular expressions
Unit 1-strings,patterns and regular expressions
Strings,patterns and regular expressions in perl
Basic perl programming
perl-pocket
perl-pocket
perl-pocket
Ad

More from mussawir20 (20)

PPT
Php Operators N Controllers
PPT
Php Calling Operators
PPT
Database Design Process
PPT
Php Simple Xml
PPT
Php Sq Lite
PPT
Php Sessoins N Cookies
PPT
Php Rss
PPT
Php Reusing Code And Writing Functions
PPT
Php Oop
PPT
Php My Sql
PPT
Php File Operations
PPT
Php Error Handling
PPT
Php Crash Course
PPT
Php Basic Security
PPT
Php Using Arrays
PPT
Javascript Oop
PPT
PPT
Javascript
PPT
Object Range
PPT
Prototype Utility Methods(1)
Php Operators N Controllers
Php Calling Operators
Database Design Process
Php Simple Xml
Php Sq Lite
Php Sessoins N Cookies
Php Rss
Php Reusing Code And Writing Functions
Php Oop
Php My Sql
Php File Operations
Php Error Handling
Php Crash Course
Php Basic Security
Php Using Arrays
Javascript Oop
Javascript
Object Range
Prototype Utility Methods(1)

Recently uploaded (20)

PDF
Five Habits of High-Impact Board Members
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
UiPath Agentic Automation session 1: RPA to Agents
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PPTX
The various Industrial Revolutions .pptx
PPTX
Chapter 5: Probability Theory and Statistics
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Flame analysis and combustion estimation using large language and vision assi...
PPTX
2018-HIPAA-Renewal-Training for executives
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Architecture types and enterprise applications.pdf
DOCX
search engine optimization ppt fir known well about this
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
STKI Israel Market Study 2025 version august
PPTX
Configure Apache Mutual Authentication
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Five Habits of High-Impact Board Members
Final SEM Unit 1 for mit wpu at pune .pptx
UiPath Agentic Automation session 1: RPA to Agents
The influence of sentiment analysis in enhancing early warning system model f...
The various Industrial Revolutions .pptx
Chapter 5: Probability Theory and Statistics
A contest of sentiment analysis: k-nearest neighbor versus neural network
Flame analysis and combustion estimation using large language and vision assi...
2018-HIPAA-Renewal-Training for executives
Hindi spoken digit analysis for native and non-native speakers
Architecture types and enterprise applications.pdf
search engine optimization ppt fir known well about this
OpenACC and Open Hackathons Monthly Highlights July 2025
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
sustainability-14-14877-v2.pddhzftheheeeee
Custom Battery Pack Design Considerations for Performance and Safety
Developing a website for English-speaking practice to English as a foreign la...
STKI Israel Market Study 2025 version august
Configure Apache Mutual Authentication
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...

Php String And Regular Expressions

  • 1. String Manipulation and Regular Expressions
  • 3. BRACKETS [ ] Shows the range of search from the string [0-9] matches any decimal digit from 0 through 9. [a-z] matches any character from lowercase a through lowercase z. [A-Z] matches any character from uppercase A through uppercase Z. [A-Za-z] matches any character from uppercase A through lowercase z.
  • 4. Quantifiers The frequency or position of bracketed character sequences and single characters can be denoted by a special character, with each special character having a specific connotation. The +, *, ?,{occurrence_range}, and $ flags all follow a character sequence:
  • 5. • p+ matches any string containing at least one p. • p* matches any string containing zero or more p’s. • p? matches any string containing zero or one p. • p{2} matches any string containing a sequence of two p’s. • p{2,3} matches any string containing a sequence of two or three p’s. • p{2,} matches any string containing a sequence of at least two p’s. • p$ matches any string with p at the end of it. Still other flags can precede and be inserted before and within a character sequence: • ^p matches any string with p at the beginning of it. • [^a-zA-Z] matches any string not containing any of the characters ranging from a through z and A through Z. • p.p matches any string containing p, followed by any character, in turn followed by another p.
  • 6. • ^.{2}$ matches any string containing exactly two characters. • <b>(.*)</b> matches any string enclosed within <b> and </b> (presumably HTML bold tags). • p(hp)* matches any string containing a p followed by zero or more instances of the sequence hp. Search a dolor Sign in a string ([\$])([0-9]+)
  • 7. Predefined Character Ranges (Character Classes) • [:alpha:]: Lowercase and uppercase alphabetical characters. This can also be specified as [A-Za-z]. • [:alnum:]: Lowercase and uppercase alphabetical characters and numerical digits. This can also be specified as [A-Za-z0-9]. • [:cntrl:]: Control characters such as a tab, escape, or backspace. • [:digit:]: Numerical digits 0 through 9. This can also be specified as [0-9]. • [:graph:]: Printable characters found in the range of ASCII 33 to 126. • [:lower:]: Lowercase alphabetical characters. This can also be specified as [a-z]. • [:punct:]: Punctuation characters, including ~ ` ! @ # $ % ^ & * ( ) - _ + = { } [ ] : ; ' < > , . ? and /. • [:upper:]: Uppercase alphabetical characters. This can also be specified as [A-Z]. • [:space:]: Whitespace characters, including the space, horizontal tab, vertical tab, new line, form feed, or carriage return. • [:xdigit:]: Hexadecimal characters. This can also be specified as [a-fA-F0-9].
  • 8. ereg() executes a case-sensitive search of string for pattern, returning TRUE if the pattern is found and FALSE otherwise. eregi() Unlike ereg(), the search is case insensitive.This function can be useful when checking the validity of strings, such as passwords. ereg_replace() The ereg_replace() function operates much like ereg(), except that the functionality is extended to finding and replacing pattern with replacement instead of simply locating it. If no matches are found, the string will remain unchanged. Like ereg(), ereg_replace() is case sensitive. eregi_replace() The eregi_replace() function operates exactly like ereg_replace(), except that the search for pattern in string is not case sensitive. PHP’s Regular Expression Functions (POSIX Extended)
  • 9. split() The split() function divides string into various elements, with the boundaries of each element based on the occurrence of pattern in string. The optional input parameter limit is used to specify the number of elements into which the string should be divided, starting from the left end of the string and working rightward. In cases where the pattern is an alphabetical character, split() is case sensitive. Here’s how you would use split() to break a string into pieces based on occurrences of horizontal tabs and newline characters: spliti() The spliti() function operates exactly in the same manner as its sibling split(), except that it is case insensitive. sql_regcase() The sql_regcase() function converts each character in string into a bracketed expression containing two characters. If the character is alphabetic, the bracket will contain both forms; otherwise, the original character will be left unchanged. This function is particularly useful when PHP is used in conjunction with products that support only case-sensitive regular expressions. Here’s how you would use sql_regcase() to convert a string:
  • 10. Practical Examples <?php $username = &quot;jasoN&quot;; if (ereg(&quot;([^a-z])&quot;,$username)) echo &quot;Username must be all lowercase!&quot;; ?> ereg() boolean ereg (string pattern , string string [, array regs ])
  • 11. <?php $url = &quot;http://www.apress.com&quot;; // break $url down into three distinct pieces: // &quot;http://www&quot;, &quot;apress&quot;, and &quot;com&quot; $parts = ereg(&quot;^(http://www)\.([[:alnum:]]+)\.([[:alnum:]]+)&quot;, $url, $regs); echo $regs[0]; // outputs the entire string &quot;http://www.apress.com&quot; echo &quot;<br>&quot;; echo $regs[1]; // outputs &quot;http://www&quot; echo &quot;<br>&quot;; echo $regs[2]; // outputs &quot;apress&quot; echo &quot;<br>&quot;; echo $regs[3]; // outputs &quot;com&quot; ?> This returns: http://www.apress.com http://www apress com
  • 12. eregi() int eregi (string pattern , string string , [array regs ]) <?php $pswd = &quot;jasongild&quot;; if (!eregi(&quot;^[a-zA-Z0-9]{8,10}$&quot;, $pswd)) echo &quot;The password must consist solely of alphanumeric characters, and must be 8-10 characters in length!&quot;; ?> In this example, the user must provide an alphanumeric password consisting of 8 to 10 characters, or else an error message is displayed.
  • 13. ereg_replace() string ereg_replace (string pattern , string replacement , string string ) <?php $text = &quot;This is a link to http://www.wjgilmore.com/.&quot;; echo ereg_replace(&quot;http://([a-zA-Z0-9./-]+)$&quot;, &quot;<a href=\&quot;\\0\&quot;>\\0</a>&quot;,$text); ?> This returns: href=&quot;http://www.wjgilmore.com/&quot;>http://www.wjgilmore.com</a>.
  • 14. split() array split (string pattern , string string [, int limit ]) <?php $text = &quot;this is\tsome text that\nwe might like to parse.&quot;; print_r(split(&quot;[\n\t]&quot;,$text)); ?> Array ( [0] => this is [1] => some text that [2] => we might like to parse. )
  • 15. sql_regcase() string sql_regcase (string string ) <?php $version = &quot;php 4.0&quot;; print sql_regcase($version); ?> Output: [Pp] [Hh] [Pp] 4.0
  • 16. Regular Expression Syntax (Perl Style) Modifiers
  • 17. Metacharacters • \A: Matches only at the beginning of the string. • \b: Matches a word boundary. • \B: Matches anything but a word boundary. • \d: Matches a digit character. This is the same as [0-9]. • \D: Matches a nondigit character. • \s: Matches a whitespace character. • \S: Matches a nonwhitespace character. • []: Encloses a character class. A list of useful character classes was provided in the previous section. • (): Encloses a character grouping or defines a back reference. • $: Matches the end of a line. • ^: Matches the beginning of a line. • .: Matches any character except for the newline.
  • 18. • \: Quotes the next metacharacter. • \w: Matches any string containing solely underscore and alphanumeric characters. This is the same as [a-zA-Z0-9_]. • \W: Matches a string, omitting the underscore and alphanumeric characters.
  • 19. Let’s consider a few examples: /sa\b/ Because the word boundary is defined to be on the right side of the strings, this will match strings like pisa and lisa, but not sand. /\blinux\b/i This returns the first case-insensitive occurrence of the word linux. /sa\B/ The opposite of the word boundary metacharacter is \B, matching on anything but a word boundary. This will match strings like sand and Sally, but not Melissa. /\$\d+\g This returns all instances of strings matching a dollar sign followed by one or more digits.
  • 20. PHP’s Regular Expression Functions (Perl Compatible) preg_grep() array preg_grep (string pattern , array input [, flags ]) The preg_grep() function searches all elements of the array input, returning an array consisting of all elements matching pattern. Consider an example that uses this function to search an array for foods beginning with p : preg_match() int preg_match (string pattern , string string [, array matches ] [, int flags [, int offset ]]]) The preg_match() function searches string for pattern, returning TRUE if it exists and FALSE otherwise. The optional input parameter pattern_array can contain various sections of the subpatterns contained in the search pattern, if applicable. Here’s an example that uses preg_match() to perform a case-sensitive search:
  • 21. preg_match_all() int preg_match_all (string pattern , string string , array pattern_array [, int order ]) The preg_match_all() function matches all occurrences of pattern in string, assigning each occurrence to array pattern_array in the order you specify via the optional input parameter order. The order parameter accepts two values: preg_quote() string preg_quote(string str [, string delimiter ]) The function preg_quote() inserts a backslash delimiter before every character of special significance to regular expression syntax. These special characters include: $ ^ * ( ) + = { } [ ] | \\ : < >. The optional parameter delimiter is used to specify what delimiter is used for the regular expression, causing it to also be escaped by a backslash.
  • 22. The preg_replace() function operates identically to ereg_replace(), except that it uses a Perlbased regular expression syntax, replacing all occurrences of pattern with replacement, and returning the modified result. The optional input parameter limit specifies how many matches should take place. Failing to set limit or setting it to -1 will result in the replacement of all occurrences. preg_replace() mixed preg_replace (mixed pattern , mixed replacement , mixed str [, int limit ]) preg_replace_callback() mixed preg_replace_callback(mixed pattern , callback callback , mixed str [, int limit ]) Rather than handling the replacement procedure itself, reg_replace_callback() function delegates the string-replacement procedure to some other user-defined function. The pattern parameter determines what you’re looking for, while the str parameter defines the string you’re searching. The callback parameter defines the name of the function to be used for the replacement task. The optional parameter limit specifies how many matches should take place. Failing to set limit or setting it to -1 will result in the replacement of all occurrences. In the following example, a function named acronym() is passed into preg_replace_callback() and is used to insert the long form of various acronyms into the target string
  • 23. preg_split() array preg_split (string pattern , string string [, int limit [, int flags ]]) The preg_split() function operates exactly like split(), except that pattern can also be defined in terms of a regular expression. If the optional input parameter limit is specified, only limit number of substrings are returned
  • 24. Practical of Perl Expressions preg_grep() array preg_grep (string pattern , array input [, flags ]) <?php $foods = array(&quot;pasta&quot;, &quot;steak&quot;, &quot;fish&quot;, &quot;potatoes&quot;); $food = preg_grep(&quot;/^p/&quot;, $foods); print_r($food); ?> This returns: Array ( [0] => pasta [3] => potatoes )
  • 25. preg_match() int preg_match (string pattern , string string [, array matches ] [, int flags [, int offset ]]]) <?php $line = &quot;Vim is the greatest word processor ever created!&quot;; if (preg_match(&quot;/\bVim\b/i&quot;, $line, $match)) print &quot;Match found!&quot;; ?> For instance, this script will confirm a match if the word Vim or vim is located, but not simplevim, vims, or evim.
  • 26. preg_match_all() int preg_match_all (string pattern , string string , array pattern_array [, int order ]) <?php $userinfo = &quot;Name: <b>Zeev Suraski</b> <br> Title: <b>PHP Guru</b>&quot;; preg_match_all (&quot;/<b>(.*)<\/b>/U&quot;, $userinfo, $pat_array); print $pat_array[0][0].&quot; <br> &quot;.$pat_array[0][1].&quot;\n&quot;; ?> This returns: Zeev Suraski PHP Guru
  • 27. preg_quote() string preg_quote(string str [, string delimiter ]) <?php $text = &quot;Tickets for the bout are going for $500.&quot;; echo preg_quote($text); ?> This returns: Tickets for the bout are going for \$500\.
  • 28. preg_replace() mixed preg_replace (mixed pattern , mixed replacement , mixed str [, int limit ]) <?php $text = &quot;This is a link to http://www.wjgilmore.com/.&quot;; echo preg_replace(&quot;/http:\/\/(.*)\//&quot;, &quot;<a href=\&quot;\${0}\&quot;>\${0}</a>&quot;, $text); ?> This returns: This is a link to <a href=&quot;http://www.wjgilmore.com/&quot;>http://www.wjgilmore.com/</a>.
  • 29. preg_replace_callback() mixed preg_replace_callback(mixed pattern , callback callback , mixed str [, int limit ]) <?php // This function will add the acronym long form // directly after any acronyms found in $matches function acronym($matches) { $acronyms = array( 'WWW' => 'World Wide Web', 'IRS' => 'Internal Revenue Service', 'PDF' => 'Portable Document Format'); if (isset($acronyms[$matches[1]])) return $matches[1] . &quot; (&quot; . $acronyms[$matches[1]] . &quot;)&quot;; else return $matches[1]; }
  • 30. This returns: The IRS (Internal Revenue Service) offers tax forms in PDF (Portable Document Format) on the WWW (World Wide Web). // The target text $text = &quot;The <acronym>IRS</acronym> offers tax forms in <acronym>PDF</acronym> format on the <acronym>WWW</acronym>.&quot;; // Add the acronyms' long forms to the target text $newtext = preg_replace_callback(&quot;/<acronym>(.*)<\/acronym>/U&quot;, 'acronym', $text); print_r($newtext);?>
  • 31. preg_split() array preg_split (string pattern , string string [, int limit [, int flags ]]) <?php $delimitedText = &quot;+Jason+++Gilmore+++++++++++Columbus+++OH&quot;; $fields = preg_split(&quot;/\+{1,}/&quot;, $delimitedText); foreach($fields as $field) echo $field.&quot;<br />&quot;; ?> This returns the following: Jason Gilmore Columbus OH
  • 32.  
  • 33.  
  • 34.  
  • 35.  
  • 36.  
  • 37.  
  • 38.  
  • 39.  
  • 40.