Your SlideShare is downloading. ×
  • Like
  • Save
PHP Regular Expressions
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

PHP Regular Expressions

  • 3,941 views
Published

 

Published in Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,941
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

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]+