tailieunhanh - PHP 5 Recipes A Problem-Solution Approach 2005 phần 7

Điều này không chỉ tác động đến tốc độ xử lý mà nó còn thường xuyên dẫn tới những cách hoạt động và những lỗi không thể giải thích trong các ứng dụng PHP 4 bởi vì nhiều nhà phát triển nghĩ rằng cả hai biến đều sẽ trỏ đến cùng một đối tượng mà điều này thì lại không hề xảy ra trên thực tế. | 388 9-20 CREATING YOUR OWN REGEXP CLASS The modified class is as follows with the additions in bold php class RegExp const POSIX POSIX const PCRE PCRE public pattern public mode Constructor Creates a new instance of the RegExp object with the pattern given. function construct pattern mode this- pattern pattern if mode Defaults to PCRE if there is no mode defined this- mode self PCRE else In a real implementation this mode should be validated-check it against the PRCE and POSIX constants this- mode mode prints the string representation of the RegExp object which in this case is the pattern function __toString return this- pattern isMatch str Returns the number of matches found so is 0 if no match is present. function isMatch str if strcmp this- mode self PCRE 0 result preg_match this- pattern str else result ereg this- pattern str return result getMatches str Returns an array of matches found in the string str function getMatches str 9-20 CREATING YOUR OWN REGEXP CLASS 389 if strcmp this- mode self PCRE 0 preg_match_all this- pattern str matches else ereg this- pattern str matches return matches replace replaceStr str Makes a replacement in a string - replaceStr The string to use as a replacement for the pattern. - str The string in which to make the replacement function replace replaceStr str if strcmp this- mode self PCRE 0 result preg_replace this- pattern replaceStr str else result ereg_replace this- pattern replaceStr str return result The strcmp function compares the mode against the PCRE constants because it is a value that needs exact matching and strcmp is a more efficient method of doing this particular comparison. The following code uses this new and improved class that supports both PCRE and POSIX regular expressions re new RegExp Hello RegExp PCRE re2 new RegExp Hello RegExp POSIX print Using PCRE n n print Pattern . re- pattern . n if re- isMatch Goodbye world echo Found match n else echo Didn t find match n if re- isMatch Hello world echo Found .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.