TAILIEUCHUNG - ICT 5 Web Development - Chapter 12: Regular Expressions
ICT 5 Web Development - Chapter 12: Regular Expressions More String functions, Regular Expression, Building an Example RE, Filter Input Data, A Full Script Example, Using grouping characters, Determine the precise field rules. | Vietnam and Japan Joint ICT HRD Program More String functions int strpos(string str, string find [, int start]) $numToPower = ‘20^2’; 20 2 ; $caretPos = strpos($numToPower, ‘^’); $num = substr($numToPower, 0, $caretPos); $power = substr($numToPower, $caretPos + 1); echo “You’re raising $num to the power of $power.”; ICT 5 Web Development string str_replace(string find, string replace, string str) Chapter 12. Regular Expressions $str = ‘My dog knows a cat that knows the ferret that stole my keys.’; $find = array(‘dog’, ‘cat’, ‘ferret’); echo str_replace($find, ‘mammal’, $str); Nguyen Thi Thu Trang trangntt@ 2 The ereg() function Why regular expressions? Scripting problem may require: Use ereg() to check if a string contains a match pattern: – verification of input from form was input a 7 digit phone number – parsing input from a file FirstName:LastName:Age:Salary PHP supports three pattern matching functions: – ereg(), split(), and ereg replace() split(), ereg_replace() ereg_replace replace() Regular expressions are used to define very specific match patterns 3 4 1 Content ereg() - example 1. Regular Expression 2. Building an Example RE 3. Filter Input Data Consider the following $name = 'J k J k $ 'Jake Jackson'; ' $pattern = 'ke'; if (ereg($pattern, $name)){ print 'Match'; } else { print 'No match'; } This code outputs “Match” since the string h d h “ke” is found. If $pattern was “aa” the above code segment would output “No match” 5 Content 6 . What are regular expressions? Special pattern matching characters with specific pattern matching meanings. meanings 1. Regular Expression 2. Building an Example RE 3. Filter Input Data – Their meanings are defined by an industry standard (the IEEE POSIX standard). – For example, a caret symbol (^) returns a match (^ when the pattern that follows starts the target string. 7 $part = 'AA100' $ t 'AA100'; $pattern = '^AA'; if (ereg($pattern, $part)) { print 'Match'; } .
đang nạp các trang xem trước