SlideShare ist ein Scribd-Unternehmen logo
1 von 40
String Manipulation and Regular Expressions
Regular Expressions (POSIX) ,[object Object]
BRACKETS [ ] ,[object Object],[object Object],[object Object],[object Object],[object Object]
Quantifiers ,[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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].
[object Object],[object Object],[object Object],[object Object],[object Object],PHP’s Regular Expression Functions (POSIX Extended)
[object Object],[object Object],[object Object],[object Object],[object Object]
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=amp;quot;0amp;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 issome text thatwe might like to parse.&quot;; print_r(split(&quot;[]&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 •  : Matches only at the beginning of the string. •  : Matches a word boundary. •  : Matches anything but a word boundary. •  : Matches a digit character. This is the same as [0-9]. •  : Matches a nondigit character. •  : Matches a whitespace character. •  : 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. •  : Matches any string containing solely underscore and alphanumeric characters. This is the same as [a-zA-Z0-9_]. •  : Matches a string, omitting the underscore and alphanumeric characters.
Let’s consider a few examples: /sa/ 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. /linux/i This returns the first case-insensitive occurrence of the word linux. /sa/ The opposite of the word boundary metacharacter is , matching on anything but a word boundary. This will match strings like sand and Sally, but not Melissa. /+ 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;/Vim/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;&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=amp;quot;{0}amp;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
 
 
 
 
 
 
 
 
 

Weitere ähnliche Inhalte

Was ist angesagt?

Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionBharat17485
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
String variable in php
String variable in phpString variable in php
String variable in phpchantholnet
 
Andrei's Regex Clinic
Andrei's Regex ClinicAndrei's Regex Clinic
Andrei's Regex ClinicAndrei Zmievski
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionMahzad Zahedi
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in pythonJohn(Qiang) Zhang
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular ExpressionsMukesh Tekwani
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle databaseAmrit Kaur
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular ExpressionsSatya Narayana
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++Anjesh Tuladhar
 
Ruby cheat sheet
Ruby cheat sheetRuby cheat sheet
Ruby cheat sheetTharcius Silva
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressionsBen Brumfield
 
Strings in Python
Strings in PythonStrings in Python
Strings in Pythonnitamhaske
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Prof. Wim Van Criekinge
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)Chirag Shetty
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsDanny Bryant
 

Was ist angesagt? (20)

Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
String variable in php
String variable in phpString variable in php
String variable in php
 
Andrei's Regex Clinic
Andrei's Regex ClinicAndrei's Regex Clinic
Andrei's Regex Clinic
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle database
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
 
Ruby cheat sheet
Ruby cheat sheetRuby cheat sheet
Ruby cheat sheet
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
 

Ähnlich wie Php String And Regular Expressions

Regex posix
Regex posixRegex posix
Regex posixsana mateen
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracleLogan Palanisamy
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfBryan Alejos
 
Ruby RegEx
Ruby RegExRuby RegEx
Ruby RegExSarah Allen
 
Regex lecture
Regex lectureRegex lecture
Regex lectureJun Shimizu
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web ProgrammingMuthuselvam RS
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex powerMax Kleiner
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expressionazzamhadeel89
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
regex.pptx
regex.pptxregex.pptx
regex.pptxqnuslv
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9Vince Vo
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Ahmed El-Arabawy
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and StringsEduardo Bergavera
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programmingAppili Vamsi Krishna
 
regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdfDarellMuchoko
 

Ähnlich wie Php String And Regular Expressions (20)

Regex posix
Regex posixRegex posix
Regex posix
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 
Ruby RegEx
Ruby RegExRuby RegEx
Ruby RegEx
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
 
Variables In Php 1
Variables In Php 1Variables In Php 1
Variables In Php 1
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
regex.pptx
regex.pptxregex.pptx
regex.pptx
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdf
 
String notes
String notesString notes
String notes
 

Mehr von mussawir20

Php Operators N Controllers
Php Operators N ControllersPhp Operators N Controllers
Php Operators N Controllersmussawir20
 
Php Calling Operators
Php Calling OperatorsPhp Calling Operators
Php Calling Operatorsmussawir20
 
Database Design Process
Database Design ProcessDatabase Design Process
Database Design Processmussawir20
 
Php Simple Xml
Php Simple XmlPhp Simple Xml
Php Simple Xmlmussawir20
 
Php Sq Lite
Php Sq LitePhp Sq Lite
Php Sq Litemussawir20
 
Php Sessoins N Cookies
Php Sessoins N CookiesPhp Sessoins N Cookies
Php Sessoins N Cookiesmussawir20
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functionsmussawir20
 
Php My Sql
Php My SqlPhp My Sql
Php My Sqlmussawir20
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operationsmussawir20
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handlingmussawir20
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Coursemussawir20
 
Php Basic Security
Php Basic SecurityPhp Basic Security
Php Basic Securitymussawir20
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arraysmussawir20
 
Javascript Oop
Javascript OopJavascript Oop
Javascript Oopmussawir20
 
Javascript
JavascriptJavascript
Javascriptmussawir20
 
Object Range
Object RangeObject Range
Object Rangemussawir20
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)Prototype Utility Methods(1)
Prototype Utility Methods(1)mussawir20
 

Mehr von mussawir20 (20)

Php Operators N Controllers
Php Operators N ControllersPhp Operators N Controllers
Php Operators N Controllers
 
Php Calling Operators
Php Calling OperatorsPhp Calling Operators
Php Calling Operators
 
Database Design Process
Database Design ProcessDatabase Design Process
Database Design Process
 
Php Simple Xml
Php Simple XmlPhp Simple Xml
Php Simple Xml
 
Php Sq Lite
Php Sq LitePhp Sq Lite
Php Sq Lite
 
Php Sessoins N Cookies
Php Sessoins N CookiesPhp Sessoins N Cookies
Php Sessoins N Cookies
 
Php Rss
Php RssPhp Rss
Php Rss
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Php Oop
Php OopPhp Oop
Php Oop
 
Php My Sql
Php My SqlPhp My Sql
Php My Sql
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handling
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
Php Basic Security
Php Basic SecurityPhp Basic Security
Php Basic Security
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Javascript Oop
Javascript OopJavascript Oop
Javascript Oop
 
Html
HtmlHtml
Html
 
Javascript
JavascriptJavascript
Javascript
 
Object Range
Object RangeObject Range
Object Range
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)Prototype Utility Methods(1)
Prototype Utility Methods(1)
 

KĂźrzlich hochgeladen

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

KĂźrzlich hochgeladen (20)

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Php String And Regular Expressions

  • 1. String Manipulation and Regular Expressions
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 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.
  • 9.
  • 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=amp;quot;0amp;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 issome text thatwe might like to parse.&quot;; print_r(split(&quot;[]&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 • : Matches only at the beginning of the string. • : Matches a word boundary. • : Matches anything but a word boundary. • : Matches a digit character. This is the same as [0-9]. • : Matches a nondigit character. • : Matches a whitespace character. • : 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. • : Matches any string containing solely underscore and alphanumeric characters. This is the same as [a-zA-Z0-9_]. • : Matches a string, omitting the underscore and alphanumeric characters.
  • 19. Let’s consider a few examples: /sa/ 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. /linux/i This returns the first case-insensitive occurrence of the word linux. /sa/ The opposite of the word boundary metacharacter is , matching on anything but a word boundary. This will match strings like sand and Sally, but not Melissa. /+ 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;/Vim/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;&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=amp;quot;{0}amp;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. Â