intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

PHP and MySQL by Example- P11

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:50

75
lượt xem
13
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'php and mysql by example- p11', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: PHP and MySQL by Example- P11

  1. Figure 12.11. Splitting with multiple delimiters. Output from Example 12.10.   Example 12.11. Explanation 1 This  is  the  string  that  will  be  split  up. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  2. 2 By  using  an  empty  delimiter,  preg_split()  will  split  up  the  string  by  its  individual   characters.  The  PREG_SPLIT_NO_EMPTY  flag  causes  the  function  to  return  an  array   without  any  empty  elements. 3 The  array  of  letters  created  by  splitting  on  an  empty  delimiter  is  displayed  as  an  array   by  the  print_r()  function,  shown  in  Figure  12.12.   Figure 12.12. Splitting up a word with the preg_split() function. Output from Example 12.11. Example 12.12.
  3. 2 $array=preg_split("/\s/", $alpha, -1, PREG_SPLIT_OFFSET_CAPTURE); echo "Splitting A Word into Letters"; print_r($array); ?> Explanation 1 This  is  the  string  we  will  be  splitting  on  line  2. 2 The  preg_split()  function  takes  a  number  of  arguments.  In  this  example,  the  first   argument  is  the  delimiter.  \s  represents  a  whitespace  character.  The  second  argument   is  the  string  that  is  being  split,  $alpha.  The  third  argument  (normally  omitted)  is  -1,   stating  that  there  is  no  limit  to  the  number  of  array  elements  that  can  be  created  when   splitting  up  this  string.  The  PREG_SPLIT_OFFSET_CAPTURE  flag  says  that  for  every  array   element  created,  the  offset  of  where  it  occurred  within  the  string  will  also  be  returned.   You  can  see  in  the  output  of  this  example  (Figure  12.13)  that  each  substring  is  an  array   element,  and  its  offset  within  the  string  is  another  array  consisting  of  two  elements,  the   array  element  (substring)  and  the  offset  position  of  where  that  substring  was  found  in   the  original  string.   Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  4. Figure 12.13. Splitting up a string with the preg_split() function. Output from Example 12.12.   Other related PHP functions are: spliti(), split(), implode(), and explode(). See Chapter 8, “Arrays,” for more on these. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  5. The preg_grep() Function Similar to the UNIX grep command, the preg_grep() function returns an array of values that match a pattern found in an array instead of a search string. You can also invert the search and get an array of all elements that do not contain the pattern being searched for (like UNIX grep -v) by using the PREG_GREP_INVERT flag. Format array preg_grep ( string pattern, array input [, int flags] )   Example: $new_array = preg_grep("/ma/", array("normal", "mama", "man","plan")); // $new_array contains: normal, mama, man $new_array=preg_grep("/ma/",array("normal","mama","man", "plan"),PREG_GREP_INVERT); // $new_array contains: plan Example 12.13. Code  View:   The preg_grep() Function The preg_grep() Function
  6. 3 After  the  array  has  been  sorted,  the  preg_grep()  function  will  search  for  the  pattern,   /Pat/,  in  each  element  of  the  array,  and  return  and  assign  the  matched  array  elements   to  another  array  called  $newarray. 4 The  count()  function  returns  the  number  of  elements  in  the  new  array;  that  is,  the   number  of  elements  where  the  pattern  /Pat/  was  found. 5 The  found  elements  are  displayed.  Note  that  the  index  values  have  been  preserved. 6 When  the  PREG_GREP_INVERT  flag  is  specified,  the  preg_grep()  function  will  match  and   return  any  elements  not  found  in  the  original  array,  as  shown  in  the  output  in  Figure   12.14.   Figure 12.14. The preg_grep() function. Output from Example 12.13. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  7. 12.2.3. Getting Control—The RegEx Metacharacters Regular expression metacharacters are characters that do not represent themselves. They are endowed with special powers to allow you to control the search pattern in some way (e.g., finding a pattern only at the beginning of the line, or at the end of the line, or if it starts with an upper- or lowercase letter). Metacharacters will lose their special meaning if preceded with a backslash. For example, the dot metacharacter represents any single character, but when preceded with a backslash is just a dot or period. If you see a backslash preceding a metacharacter, the backslash turns off the meaning of the metacharacter, but if you see a backslash preceding an alphanumeric character in a regular expression, then the backslash is used to create a metasymbol. A metasymbol provides a simpler form to represent some of regular expression metacharacters. For example, [0-9] represents numbers in the range between 0 and 9, and \d represents the same thing. [0-9] uses the bracketed character class, whereas \d is a metasymbol (see Table 12.6). Table 12.6. Metacharacters Character  Class What  It  Matches Metacharacter Single  characters  and  digits   Matches  any  character  except  a  newline.     .     [a-z0-9] (for  more,  see  “Matching   Matches any single character in a set. [^a-z0-9] Single  Characters  and   Matches  any  single  character  not  in  a  set. Digits”  on  page  524) Single  characters  and  digits   Matches  one  digit.     \d     \D —Metasymbols  (for  more,   Matches a nondigit, same as [^0-9]. \w see  “Metasymbols”  on  page   Matches an alphanumeric (word) character. \W 530) Matches  a  nonalphanumeric  (nonword)   character. Whitespace  characters Matches  whitespace  character,  spaces,  tabs,   \s and  newlines.   Matches  a  nonwhitespace  character. \S   Matches  a  newline. \n   Matches  a  return. \r   Matches  a  tab. \t   Matches  a  form  feed. \f   Matches  a  null  character \0 Anchored  characters  (for   Matches  a  word  boundary.     \b     \B more  see  “Anchoring   Matches a nonword boundary. ^ Metacharacters”  on  page   Matches to beginning of line. Matches to end of line. $ 520) Matches the beginning of the string only. \A \D Matches  the  end  of  the  string  or  line. Repeated  characters  (for   Matches  0  or  1  occurrences  of  the  letter  x. x? more,  see  “Metacharacters   to  Repeat  Pattern  Matches”   Matches  0  or  more  occurrences  of  the  letter   x* on  page  533) x. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  8. Table 12.6. Metacharacters Character  Class What  It  Matches Metacharacter   Matches  1  or  more  occurrences  of  the  letter   x+ x. Grouped  characters  (for   Matches  one  or  more  patterns  of  xyz  (e.g.,   (xyz)+ more,  see  “Grouping  or   xyxxyzxyz). Clustering.”  on  page  544) Matches  at  least  m  occurrences  of  the  letter  x,   x{m,n} and  no  more  than  n  occurrences  of  the  letter   x. Alternative  characters  (for   Matches  one  of  was,  were,  or  will. was|were|will more,  see  “Metacharacters   for  Alternation”  on  page   543) Remembered  characters   Used  for  backreferencing.     (string)     \1 or $1 (for  more,   Matches first set of parentheses. Matches second set of parentheses. \2 or $2 see“Remembering  or   Matches  third  set  of  parentheses. \3  or  $3 Capturing”  on  page  545) Positive  lookahead  and   Matches  x  but  does  not  remember  the  match.   (?:x) lookbehind  (for  more,  see   These  are  called  noncapturing  parentheses. “Positive  Lookahead”  on   page  550  and  “Positive   Matches  x  only  if  x  is  followed  by  y.  For   x(?=y) Lookbehind”  on  page  552 example,  /Jack(?=Sprat)/  matches  Jack   only  if  it  is  followed  by  Sprat.   /Jack(?=Sprat|Frost)/  matches  Jack  only  if   it  is  followed  by  Sprat  or  Frost.  Neither   Sprat  nor  Frost  is  kept  as  part  of  what  was   matched.   Matches  x  only  if  x  is  not  followed  by  y.  For   x(?!y) example,  /\d+(?!\.)/  matches  one  or  more   numbers  only  if  they  are  not  followed  by  a   decimal  point.   The following regular expression contains metacharacters: /^a...c/   The first metacharacter is a caret (^). The caret metacharacter matches for a string only if it is at the beginning of the line. The period (.) is used to match for any single character, including a space. This expression contains three periods, representing any three characters. To find a literal period or any other character that does not represent itself, the character must be preceded by a backslash to prevent interpretation. The expression reads: Search at the beginning of the line for a letter a, followed by any three single characters, followed by a letter c. It will match, for example, abbbc, a123c, a c, aAx3c, and so on, only if those patterns were found at the beginning of the line. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  9. In the following examples, we perform pattern matches, searches, and replacements based on the data from a text file called data.txt. In the PHP program, the file will be opened and, within a while loop, each line will be read. The functions discussed in the previous section will be used to find patterns within each line of the file. The regular expressions will contain metacharacters, described in Table 12.6. Anchoring Metacharacters Often it is necessary to find a pattern only if it is found at the beginning or end of a line, word, or string. The “anchoring” metacharacters (see Table 12.7) are based on a position just to the left or to the right of the character that is being matched. Anchors are technically called zero-width assertions because they correspond to positions, not actual characters in a string; for example, /^abc/ means find abc at the beginning of the line, where the ^ represents a position, not an actual character. Table 12.7. Anchors (Assertions) Metacharacter What  It  Matches ^ Matches  to  beginning  of  line  or  beginning  of  string. $ Matches  to  end  of  line  or  end  of  string. \A Matches  the  beginning  of  a  string. \b Matches  a  word  boundary. \B Matches  a  nonword  boundary. \D Matches  the  end  of  a  string.   Beginning-of-Line Anchor The ^ metacharacter is called the beginning-of-line anchor. It is the first character in the regular expression and matches a pattern found at the beginning of a line or string. Example 12.14. Code  View:   (The file data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ------------------------------------------------------------------ (The PHP Program) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  10. ------------------------------------------------------------------ (Output) Betty Boop 200 BB Kingson 803 Explanation 1 The  file  data.txt  is  opened  for  reading. 2 As  long  as  the  end  of  file  has  not  been  reached,  the  while  loop  will  continue  to   execute. 3 For  each  iteration  of  the  loop,  the  fgets()  function  reads  in  a  line  of  text. 4 The  preg_match()  function  will  return  TRUE  if  a  pattern  consisting  of  a  string   beginning  with  a  B  is  matched. 5 The  lines  that  matched  are  printed. End-of-Line Anchor The end-of-line anchor, a dollar sign, is used to indicate the ending position in a line. The dollar sign must be the last character in the pattern, just before the closing forward slash delimiter of the regular expression, or it no longer means “end-of-line anchor.”[1] [1] If moving files between Windows and UNIX, the end-of-line anchor might not work. You can use programs such as dos2unix to address this problem. Example 12.15. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Program) ---------------------------------------------------------------- (Output) Steve Blenheim 100 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  11. Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 Explanation 1 The  file  data.txt  is  opened  for  reading. 2 As  long  as  the  end  of  file  hasn’t  been  reached,  the  while  loop  will  continue  to   execute. 3 For  each  iteration  of  the  loop,  the  fgets()  function  reads  in  a  line  of  text. 4 The  preg_match()  function  will  return  TRUE  if  a  pattern  consisting  of  a  line   ending  with  a  0  is  matched.  The  $  metacharacter  indicates  that  0  must  be   followed  by  a  newline. 5 The  lines  that  matched  are  printed. Word Boundaries A word boundary is represented in a regular expression by the metasymbol \b. You can search for the word that begins with a pattern, ends with a pattern, or both begins and ends with a pattern; for example, /\blove/ matches a word beginning with the pattern love, and would match lover, loveable, or lovely, but would not find glove. /love\b/ matches a word ending with the pattern love, and would match glove, clove, or love, but not clover. /\blove\b matches a word beginning and ending with the pattern love, and would match only the word love. Example 12.16. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Script) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  12. ---------------------------------------------------------------- (The Output) Mama Bear 702 Explanation 1 The  preg_match()  function  will  return  TRUE  if  a  pattern  consisting  of  the  word   bear  is  matched,  and  it  is  insensitive  to  case.  Because  the  regular  expression  is   anchored  on  both  ends  of  the  word  with  the  word  boundary  metasymbol,  \b,   only  bear  is  matched  in  $test,  not  “unbearable,”  “beard,”  or  “bears.” 2 The  lines  that  matched  are  printed. Matching Single Characters and Digits There are metacharacters to match single characters or digits, and single noncharacters or nondigits, whether in or not in a set. The Dot Metacharacter The dot metacharacter matches for any single character with exception to the newline character. For example, the regular expression /a.b/ is matched if the string contains a letter a, followed by any one single character (except the \n), followed by a letter b, whereas the expression /.../ matches any string containing at least three characters. To match on a literal period, the dot metacharacter must be preceded by a backslash; for example, /love\./ matches on love. not lover. Example 12.17. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Program) ---------------------------------------------------------------- (Output) Jon DeLoach 500 Explanation 1 The  file  data.txt  is  opened  for  reading. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  13. 2 As  long  as  the  end  of  file  has  not  been  reached,  the  while  loop  will  continue  to   execute. 3 For  each  iteration  of  the  loop,  the  fgets()  function  reads  in  a  line  of  text. 4 The  regular  expression  /^... /  contains  the  dot  metacharacter.  The  regular   expression  means:  go  to  the  beginning  (^)  of  the  line  and  find  any  three   characters,  followed  by  a  space.  (The  dot  metacharacter  does  not  match  the   newline  character.)  The  only  line  that  matched  the  pattern  starts  with  Jon.  It   begins  with  three  characters  followed  by  a  space. Example 12.18. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Program) ---------------------------------------------------------------- (Output) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Daniel DeLoach 500 Karen Evich 600 BB Kingson 803 Explanation 1 The  text  file  data.txt  is  opened  for  reading. 2 Until  the  end  of  the  file  is  reached,  the  while  loop  will  continue  looping,   reading  in  one  line  at  a  time  from  the  file. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  14. reading  in  one  line  at  a  time  from  the  file. 3 The  first  argument  to  the  preg_replace()  function  is  a  regular  expression   containing  the  dot  metacharacter.  If  the  regular  expression  (a  capital  J   followed  by  at  least  two  characters)  is  matched  in  $text,  the  found  pattern   will  be  replaced  with  Daniel. The Character Class A character class represents one character from a set of characters. For example, [abc] matches either an a, b, or c; [a-z] matches one character from a set of characters in the range from a to z; and [0-9] matches one character in the range of digits between 0 to 9. If the character class contains a leading caret ^, then the class represents any one character not in the set; for example, [^a-zA-Z] matches a single character not in the range from a to z or A to Z, and [^0-9] matches a single digit not in the range between 0 and 9 (see Table 12.8). Table 12.8. Character Classes Metacharacter What  It  Matches [abc] Matches  an  a  or  b  or  c. [a–z0–9_] Matches  any  single  character  in  a  set. [^a–z0–9_] Matches  any  single  character  not  in  a  set.   PHP provides additional metasymbols to represent a character class. The symbols \d and \D represent a single digit and a single nondigit, respectively (the same as [0-9] and [^0-9]); \w and \W represent a single word character and a single nonword character, respectively (the same as [A-Za-z_0-9] and [^A-Za-z_0-9]). If you are searching for a particular character within a regular expression, you can use the dot metacharacter to represent a single character, or a character class that matches on one character from a set of characters. In addition to the dot and character class, PHP supports some backslashed symbols (called metasymbols) to represent single characters. Matching One Character from a Set A regular expression character class represents one character out of a set of characters, as shown in Example 12.19. Example 12.19. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Program)
  15. 5 echo "$text"; } } ?> ---------------------------------------------------------------- (Output) Betty Boop 200 Igor Chevsky 300 Karen Evich 600 BB Kingson 803 Explanation 1 The  file  data.txt  is  opened  for  reading. 2 As  long  as  the  end  of  file  has  not  been  reached,  the  while  loop  will  continue  to   execute. 3 For  each  iteration  of  the  loop,  the  fgets()  function  reads  in  a  line  of  text. 4 The  regular  expression  /^[BKI]/  contains  a  character  class  matching  a  string   that  contains  a  single  uppercase  character  from  the  set  [BKI]  meaning:  a  B  or  K   or  I.  The  preg_match()  function  will  return  TRUE  if  the  pattern  is  matched. 5 These  lines  begin  with  one  of  the  three  characters  B  or  K  or  I. Matching One Character in a Range A character class can also be represented as a range of characters by placing a dash between two characters, the first being the start of the range and the second the end of the range; for example, [0-9] represents one character in the range between 0 and 9 and [A-Za-z0-9] represents one alphanumeric character. If you want to represent a range between 10 and 13, the regular expression would be /1[0-3]/, not /[10-13]/ because only one character can be matched in a character class. Example 12.20. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Program)
  16. 5 echo "$text"; } } ?> ---------------------------------------------------------------- (Output) Mama Bear 702 Igor Chevsky 300 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 Explanation 1 The  file  data.txt  is  opened  for  reading. 2 As  long  as  the  end  of  file  has  not  been  reached,  the  while  loop  will  continue  to   execute. 3 For  each  iteration  of  the  loop,  the  fgets()  function  reads  in  a  line  of  text. 4 The  regular  expression  /[E-M]/  contains  a  character  class  matching  a  string   that  contains  a  single  character  from  the  range  of  characters  between  E  and  M.   The  preg_match()  function  will  return  TRUE  if  the  pattern  is  matched. 5 Each  of  these  lines  contain  an  uppercase  letter  in  the  range  between  E  and  M. Example 12.21. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Program) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  17. ---------------------------------------------------------------- (Output) Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Explanation 1 The  text  file  data.txt  is  opened  for  reading. 2 Until  the  end  of  the  file  is  reached,  the  while  loop  will  continue  looping,   reading  in  one  line  at  a  time  from  the  file. 3 The  first  argument  to  the  preg_match()  function  is  a  regular  expression   containing  character  classes  using  a  range,  [a-z]  and  [0-9].  The  function  will   return  TRUE  if  the  pattern  is  matched  in  $text;  that  is,  one  lowercase  letter  in   the  range  from  a  to  z,  a  space,  and  a  digit  between  0  and  9. Matching One Character Not in a Set When a character set contains a caret right after the opening square bracket, then the search is inversed; that is, the regular expression represents one character not in the set or in the range. For example, [^a-z] represents one character that is not in the range between a and z. Example 12.22. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Program) ---------------------------------------------------------------- (Output) Mama Bear 702 Steve Blenheim 100 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  18. Norma Cord 400 Jon DeLoach 500 Explanation 1 The  text  file  data.txt  is  opened  for  reading. 2 Until  the  end  of  the  file  is  reached,  the  while  loop  will  continue  looping,   reading  in  one  line  at  a  time  from  the  file. 3 The  first  argument  to  the  preg_match()  function  is  a  regular  expression   containing  character  classes  using  a  range,  [^BKI]].  The  function  will  return   TRUE  if  the  pattern  is  matched  in  $text;  that  is,  the  line  begins  with  one   character  that  is  not  a  B  or  K  or  I.  The  ^  means  “not”  when  enclosed  in  square   brackets  as  part  of  a  character  set. Metasymbols Metasymbols offer an alternative way to represent a character class or whitespace characters (see Table 12.9). For example, instead of representing a number as [0-9], it can be represented as \d, and the alternative for representing a nonnumber [^0-9] is \D. Metasymbols are easier to use and and to type. Table 12.9. Metasymbols Character   Symbol What  It  Matches Class \d One  digit [0-9] \D One  nondigit [^0-9] \w One  word  character [A-Za-z0-9_] \W One  nonword  character [^A-Za-z0-9] \s One  whitespace  character  (tab,  space,  newline,  carriage  return,     form  feed,  vertical  tab) \S One  nonspace  character     Metasymbols Representing Digits and Spaces The character class [0-9] represents one digit in the range between 0 and 9, as does the metasymbol \d. To create a regular expression that matches on three digits, you could write /[0-9][0-9][0-9]/ or simply /\d\d\d/. To represent a space, you can either insert a blank space, or use the metasymbol \s. Example 12.23. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  19. Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Program) ---------------------------------------------------------------- (Output) Jon DeLoach 500 Karen Evich 600 Explanation 1 The  text  file  data.txt  is  opened  for  reading. 2 Until  the  end  of  the  file  is  reached,  the  while  loop  will  continue  looping,   reading  in  one  line  at  a  time  from  the  file. 3 The  first  argument  to  the  preg_match()  function  is  a  regular  expression   containing  the  metasymbol  \s  representing  a  space,  and  \d  representing  a   digit.  The  function  will  return  TRUE  if  the  pattern  is  matched  in  $text;  that  is,   the  line  contains  an  h,  followed  by  a  space,  and  three  digits. Metasymbols Representing Alphanumeric Word Characters The metasymbol to represent one alphanumeric word character is \w, much easier to write than [a-zA-Z0-9_]. To represent not one alphanumeric character, you simply capitalize the metasymbol, \W, which is the same as [^a-zA- Z0-9_]. Example 12.24. Code  View:   (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ---------------------------------------------------------------- (The PHP Program)
  20. 2 while( ! feof($fh)){ $text = fgets($fh); 3 if(preg_match("/^\w\w\w\W/",$text)){ echo "$text"; } } ?> ---------------------------------------------------------------- (Output) Jon DeLoach 500 Explanation 1 The  text  file  data.txt  is  opened  for  reading. 2 Until  the  end  of  the  file  is  reached,  the  while  loop  will  continue  looping,   reading  in  one  line  at  a  time  from  the  file. 3 The  first  argument  to  the  preg_match()  function  is  a  regular  expression   containing  three  alphanumeric  word  characters,  \w\w\w.  The  \w  represents  the   character  class  [A-Za-z0-9_].  The  metasymbol  \W  represents  the  character   class  [^A-Za-z0-9_].  The  function  will  return  TRUE  if  the  pattern  is  matched  in   $text;  that  is,  the  line  begins  with  three  alphanumeric  word  characters,   followed  by  a  character  that  is  not  an  alphanumeric  character. Example 12.25. Code  View:     (The File data.txt Contents) Mama Bear 702 Steve Blenheim 100 Betty Boop 200 Igor Chevsky 300 Norma Cord 400 Jon DeLoach 500 Karen Evich 600 BB Kingson 803 ------------------------------------------------------------------ (The PHP Program) ------------------------------------------------------------------ (Output) MamaXXear 702 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2