tailieunhanh - Beginning PHP 5.3 phần 8
Phần III: Sử dụng PHP trong thực tế Bạn cũng có thể vượt qua một loạt các chuỗi mục tiêu cho preg_replace () để làm việc trên, giống như sử dụng preg_grep (). Nếu bạn làm điều này, preg_replace () trả về mảng các chuỗi với bất kỳ văn bản phù hợp thay thế bằng văn bản thay thế: | Part III Using PHP in Practice You can also pass an array of target strings for preg_replace to work on much like using preg_grep . If you do this preg_replace returns the array of strings with any matched text replaced by the replacement text text array Mouse mat Keyboard cover Screen protector newText preg_replace d . d 2 Only 0 text echo pre print_r newText echo pre This code displays Array 0 Mouse mat Only 1 Keyboard cover Only 2 Screen protector Only preg_replace has a couple more tricks up its sleeve. You can pass an array of regular expression strings to the function and it will match and replace each expression in turn with the replacement string text The wholesale price is . . The product will be released on Jan 16 2010. patterns array d . d 2 w 3 d 1 2 d 4 echo preg_replace patterns CENSORED text This script outputs the following The wholesale price is CENSORED . The product will be released on CENSORED . If you also pass an array of replacement strings the matched text from each expression in the expressions array is replaced by the corresponding string in the replacements array text The wholesale price is . . The product will be released on Jan 16 2010. patterns array d . d 2 w 3 d 1 2 d 4 558 Chapter 18 String Matching with Regular Expressions replacements array PRICE CENSORED DATE CENSORED echo preg_replace patterns replacements text This script displays The wholesale price is PRICE CENSORED . The product will be released on DATE CENSORED . If your replacements array contains fewer elements than your expressions array matched text for any expression without a corresponding replacement is replaced with an empty string. For example text The wholesale price is . . The product will be released on Jan 16 2010. patterns array d . d 2 w 3 d 1 2 d 4 replacements array Price CENSORED echo preg_replace patterns replacements text displays The wholesale price is PRICE CENSORED . The product will be released on . .
đang nạp các trang xem trước