• Save
PHP Regular Expressions
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

PHP Regular Expressions

on

  • 4,755 views

 

Statistics

Views

Total Views
4,755
Views on SlideShare
4,754
Embed Views
1

Actions

Likes
4
Downloads
0
Comments
0

1 Embed 1

http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

PHP Regular Expressions Presentation Transcript

  • 1. Introduction PHP and REGEX Jussi Pohjolainen TAMK University of Applied Sciences
  • 2. Regular Expressions
    • Designed to help working with strings in UNIX environment
    • Easy way to find a pattern in a string and/or replace it if you want
    • Very powerful tool
    • Two types: POSIX and Perl Compatible
  • 3. Basic Syntax
    • In Regex, one uses patterns
    • Pattern can hold
      • Normal characters
      • Start and end indicators as ^ and $
      • Count indicators like +, - , ?
      • Logical operators, like |
      • Grouping with {}, (), []
    • Example (Perl-compatible)
      • /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$/
  • 4. ereg Functions in PHP
    • ereg-functions uses POSIX regex:
      • int ereg (string pattern, string subject [, array groups])
      • int eregi (string pattern, string subject [, array groups])
      • string ereg_replace (string pattern, string replacement, string subject)
      • string eregi_replace (string pattern, string replacement, string subject)
      • array split (string pattern, string subject [, int limit])
      • array spliti (string pattern, string subject [, int limit])
  • 5. Example
    • // POSIX REGEX
    • $pattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$";
    • $email = "jack@tamk.com";
    • if ( eregi($pattern, $email) )
    • print "Match";
    • else
    • print "Not match";
  • 6. Example 2
    • // POSIX REGEX
    • $pattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$";
    • $email = $_GET["e-mail"];
    • if ( eregi($pattern, $email) )
    • // Save user input to database
    • else
    • print "Give proper e-mail address!";
  • 7. preg function set
    • Perl compatible REGEX:
      • int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )
      • mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit [, int &$count ]] )
      • array preg_split ( string $pattern , string $subject [, int $limit [, int $flags ]] )
  • 8. Example
    • // PERL REGEX
    • $pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$/i";
    • $email = "jack@tamk.com";
    • if ( preg_match($pattern, $email) )
    • print "Match";
    • else
    • print "Not match";
  • 9. Perl Regex Examples
    • Basic usage: /pattern/modifiers
    • Examples:
      • /ha jo/ matches "tur ha jo no"
      • /ha jo/i matches "tur ha JO NO";
  • 10. Some PERL regex rules REGEX Explanation Example . Any single character /a.c/ matches "abc" or "afc" [ ] Single character that is contained within the brackets /[abc]/ matches "a", or "b" or "c" ^ Find from the beginning of the string /^a/ matches " a ku ankka" $ Find from the end of the string /a$/ matches "aku ankk a " | OR /cat|dog/ matches "dog" or "cat" ^ NOT (when used in middle of pattern) /a^aa/ matches "aba" {n,m} Matches min n, max m /a{1,3}/ matches "a", or "aa", or "aaa" * Matches 0-n /a*/ ? Matches 0-1 /(hello)?/ + Matches 1-n /a+/
  • 11. Examples
    • /.ala/
      • ka vala mpi
    • /. Älä/
      • Nyt se loppui . Älä rupea.
    • /^begin/
      • begin starts like this.
    • /end$/
      • this is the end
  • 12. Examples
    • /^begin and end$/
      • begin and end
    • /one|two|three/
      • this is the one
    • /[abc]$/
      • ?
    • /^Hello my name is ^(Jussi)$/i
      • ?
  • 13. More Perl REGEX Rules REGEX Explanation w [a-zA-Z0-9_] W ^ [a-zA-Z0-9_] New Line s Whitespace / Empty S Not whitespaces / Empty d Number D Not Number
  • 14. Examples
    • /^[+-]?d+.?d*$/
    • /^d{1,2}.d{1,2}.?d{4}?$/
    • /^(d{1,2}):(d{1,2}).(d{1,2})$/
    • /[CDE]:w]+w]+