Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.

Similar presentations


Presentation on theme: "Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting."— Presentation transcript:

1 Lesson 4 String Manipulation

2 Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting to validate user data or extract data from text files, you need to know how to handle strings. Basic String Function substr() The substr() function returns a part of a string. It receives 3 arguments : substr( string source, int begin, int length) the first character is counted as zero The third argument is optional

3 String Manipulation Lesson 4

4 String Manipulation Lesson 4 trim() Useful for cleaning up user input, trim() simply strips the white space characters (spaces, tabs, new lines) from the beginning and the end of a strings and returns the trimmed strings If you wish to only trim the beginning of the string, use ltrim(). To trim the end of a string, use chop()

5 String Manipulation Lesson 4 strlen() Returns the number of characters in a string

6 String Manipulation Lesson 4 chr() chr() receives an integer that represents an ASCII code, and returns the corresponding character. echo (chr(34)); // Prints a quotation mark “ This is equivalent to: echo ("\""); // Prints a quotation mark

7 String Manipulation Lesson 4 ord() ord() is chr()'s complement. It receives a character and returns the corresponding ASCII code as an integer. if ($c != ord(9) && $c != ord(13)) { // Only append $c if not tab or enter $a.= $c; }

8 String Manipulation Lesson 4 ord() ord() is chr()'s complement. It receives a character and returns the corresponding ASCII code as an integer. if ($c != ord(9) && $c != ord(13)) { // Only append $c if not tab or enter $a.= $c; }

9 String Manipulation Lesson 4 number_format() The function takes one, two, or four arguments (three arguments will result in An error). if only the first argument is used, num depicted as an integer with commas separating the thousands.

10 String Manipulation Lesson 4 number_format() if the first two arguments are used, the number will be shown with precision digits after the decimal point. The decimal point will be represented as a dot and commas will separate the thousands:

11 String Manipulation Lesson 4 number_format()

12 Lesson 4 Regular Expressions

13 Lesson 4 Regular Expressions is essentially a pattern, a set of characters that describes the nature of the string being Sought. The pattern can be a simple as a literal string, or it can be extremely complex. Using special characters to represent ranges of characters, multiple occurrences, or Specific contexts in which to search.

14 Regular Expressions Lesson 4 ^adam This pattern includes the special character ^, which indicates that the pattern should only match for strings that begin with the string “adam”, so the string “adam is the CEO” Would match this pattern, but string “ Mr.adam is the CEO” would not. Adam$ The $ character is used to match strings that end with the given pattern. Hello Mr.adam…….. match Hello Mr.adams…….. Doesn’t match

15 Lesson 4 Regular Expressions

16 Lesson 4 Regular Expressions

17 Lesson 4 ^adam This pattern includes the special character ^, which indicates that the pattern should only match for strings that begin with the string “adam”, so the string “adam is the CEO” Would match this pattern, but string “ Mr.adam is the CEO” would not. Adam$ The $ character is used to match strings that end with the given pattern. Hello Mr.adam…….. match Hello Mr.adams…….. Doesn’t match

18 Regular Expressions Lesson 4

19 Character Classes Lesson 4 In internet applications, regular expressions are especially useful for validating user input. You want to make sure that when a user submits a form, his or her phone number,Email address, credit card number, etc. all make reasonable sense. We need a way to describe the values that we are trying to match, and character classes provide a way to do that.

20 Character Classes Lesson 4 To create a character class that matches any one vowel, we place all vowels in square brackets.

21 Character Classes Lesson 4

22 Character Classes Lesson 4 [a-z] match any lowercase letter [A-Z] match any uppercase letter [a-zA-Z] match any letter [0-9] match any digit [0-9\.\-] match any digit, dot, or minus sign Be aware that each of these classes is used to match one character If you were attempting to match a string composed of one lowercase letter and one Digit only such as “a2”, ”t6”, “b1”, you could use ^[a-z][0-9]$

23 Character Classes Lesson 4

24 Character Classes Lesson 4 When “^” used inside the brackets of a character class, it means “not” or “exclude” ^[^0-9][0-9]$ The first character would be any non-digit character [^a-z] //Any character that is not a lowercase letter

25 Character Classes Lesson 4

26 Character Classes Lesson 4 [[:alpha:]] Any letter [[:digit:]] Any digit [[:alnum:]] Any letter or digit [[:space:]] Any white space [[:upper:]] Any uppercase letter [[:lower:]] Any lowercase [[:punct:]] Any punctuation mark

27 Character Classes Lesson 4

28 Character Classes Lesson 4 Detecting Multiple Occurrences We know now how to match a letter or digit, a word consists of one or more letters And a number consist of one or more digits, curly braces {} can be used to match Occurrence of the characters.

29 Character Classes Lesson 4 These examples demonstrate the three different usage of {}. With a single integer, {x} means “match exactly x occurrences of the previous character. with one integer and a comma, {x,} means “match x or more occurrences of the previous character”; And with two comma separated integers {x,y} means “match the previous character if it occures at least x times, but no more than y times”

30 Character Classes Lesson 4 Detecting Multiple Occurrences Character Class Description ^[a-zA-Z_]$ match any letter or underscore ^[[:alpha:]]{3}$ match any three letter word ^a$ match a ^a{4}$ match : aaaa ^a{2,4}$ match : aa or aaa or aaaa ^a{1,3}$ match a,aa, or aaa ^a{2,}$ match a string containing 2 or more a’s ^a{2,} match aasad,aaamad, but not apa a{2,} match saad,maaad but not bababa

31 Character Classes Lesson 4

32 Character Classes Lesson 4

33 Character Classes Lesson 4

34 Character Classes Lesson 4

35 Character Classes Lesson 4

36 Character Classes Lesson 4

37 Character Classes Lesson 4 ? Is equivalent to {0,1} + is equivalent to {1,} * is equivalent to {0,}.+ one or more if any character expect new line ^[a-zA-Z0-9_]+$ //match any word of at least one letter,number or _ ^[0-9]+$ //match any positive integer number ^\-?[0-9]+$ //match any integer number ^\-?[0-9]*\.?[0-9]*$ //match any double

38 Character Classes Lesson 4.+ one or more if any character expect new line Now lets validate email address ^. + @.+ \..+ $ Begin one or more char @ one or more char. one or more char End adam@solutions.com

39 Character Classes Lesson 4

40 Character Classes Lesson 4 adams+ //matchs adams, adamss, adamssss…. (adam)+ // matchs adam,adamadam,adamadamadam ……… (Mad|Ad)am$ //matches Madam or Adam Am$ //matches the Am at the end of the string Jamal|adam$ //matches Jamal anywhere or adam at the end of the string (Jamaladam)$ //matches either Jamal or adam at the end


Download ppt "Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting."

Similar presentations


Ads by Google