Xử lí chuỗi trong PHP

13 691 0
Xử lí chuỗi trong  PHP

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Danh sách 98 hàm php dùng trong xử lí chuỗi, có hướng dẫn cách sử dụng, cấu trúc hàm, ví dụ dễ hiểu kèm theo có kết quả ví dụ giúp bạn dễ tiếp thu môn lập trình php. Bạn có thể xem thêm các ví dụ trên trang chủ của php http:php.net

PHP – 98 HÀM XỬ LÍ CHUỖI 1. addcslashes($str, $char_list) — Hàm này sẽ thêm dấu gạch chéo (\) đằng trước những ký tự trong chuỗi $str mà ta liệt kê ở $char_list. <?php echo (addcslashes('PHPnet', 'a z')); // Kết quả: \n\e\t echo (addcslashes('PHPnet', 'a zA Z')); // a zA Z là gồm các từ từ a => z và A => Z // Kết quả: \P\H\P\n\e\t ?> 2. addslashes($str) — Thêm dấu gách chéo trước những ký tự (‘, “, \) trong chuỗi $str. <?php echo addslashes(“Wellcom to ‘PHP’”); //Kết quả: Wellcom to \’PHP\’ echo addslashes( Wellcom to “PHP”’� ); //Kết quả : Wellcom to \”PHP\” ?> 3. bin2hex($str) — Chuyển đổi dữ liệu dạng nhị phân sang dạng biểu diễn hệ hexa. <?php $str = bin2hex("Hello World!"); echo($str); //Kết quả: 48656c6c6f20576f726c6421 ?> 4. chop($str) — Loại bỏ những khoảng trắng ở cuối chuỗi. <?php $str = "php "; echo chop($str); /* Kết quả php*/ ?> 5. chr($acii)— Cho một kí tự đặc biệt trong bảng mã ASCII <?php echo chr(161); // Kết quả: A ?> 6. chunk_split($str, $len, $end) — Tách xâu $str thành các xâu nhỏ hơn với độ dài mỗi xâu là $len và kết thúc bằng xâu $end <?php $str = "Hello world!"; echo chunk_split($str,1,"."); //Kết quả: H.e.l.l.o. .w.o.r.l.d.!. ?> 7. convert_cyr_string — Chuyển đổi từ một kí tự Cyrillic sang một kí tự khác <?php $str = "Hello world! æøå"; echo $str . "<br>"; echo convert_cyr_string($str,'w','a'); //Chuyển kí tự "w" (windows-1251) thành "a" (x-cp866) // Kết quả: //Hello world! æøå //Hello world! ¦è¥ ?> 8. convert_uudecode — Decode a uuencoded string <?php $str = ",2&5L;&\@=V]R;&0A `"; echo convert_uudecode($str);// Hello world! ?> by Grimes 1 9. convert_uuencode — Uuencode a string <?php $str = "Hello world!"; echo convert_uuencode($str);// ,2&5L;&\@=V]R;&0A ` ?> 10. count_chars — Cho thông tin về các kí tự dùng trong xâu <?php $str = "Hello World!"; echo count_chars($str,3);// !HWdelor ?> 11. crc32 — Tính toán sự thừa vòng đa thức của một xâu <?php $str = crc32("Hello World!"); printf("%u\n",$str);// 472456355 ?> 12. crypt — :Một cách mã hóa một xâu <?php // 2 character salt if (CRYPT_STD_DES == 1) { echo "Standard DES: ".crypt('something','st')."\n<br>"; } else { echo "Standard DES not supported.\n<br>"; } // 4 character salt if (CRYPT_EXT_DES == 1) { echo "Extended DES: ".crypt('something','_S4 some')."\n<br>"; } else { echo "Extended DES not supported.\n<br>"; } // 12 character salt starting with $1$ if (CRYPT_MD5 == 1) { echo "MD5: ".crypt('something','$1$somethin$')."\n<br>"; } else { echo "MD5 not supported.\n<br>"; } // Salt starting with $2a$. The two digit cost parameter: 09. 22 characters if (CRYPT_BLOWFISH == 1) { echo "Blowfish: ".crypt('something','$2a$09$anexamplestringforsalt$')."\n<br>"; } else { echo "Blowfish DES not supported.\n<br>"; by Grimes 2 } // 16 character salt starting with $5$. The default number of rounds is 5000. if (CRYPT_SHA256 == 1) { echo "SHA-256: ".crypt('something','$5$rounds=5000$anexamplestringforsalt$')."\n<br>"; } else { echo "SHA-256 not supported.\n<br>"; } // 16 character salt starting with $5$. The default number of rounds is 5000. if (CRYPT_SHA512 == 1) { echo "SHA-512: ".crypt('something','$6$rounds=5000$anexamplestringforsalt$'); } else { echo "SHA-512 not supported."; } ?> Kết quả: Standard DES: stqAdD7zlbByI Extended DES: _S4 someQXidlBpTUu6 MD5: $1$somethin$4NZKrUlY6r7K7.rdEOZ0w. Blowfish: $2a$09$anexamplestringforsaleLouKejcjRlExmf1671qw3Khl49R3dfu SHA-256: $5$rounds=5000$anexamplestringf$KIrctqsxo2wrPg5Ag/hs4jTi4PmoNKQUGWFXlVy9vu9 SHA-512: $6$rounds=5000$anexamplestringf$Oo0skOAdUFXkQxJpwzO05wgRHG0dhuaPBaOU/ oNbGpCEKlf/7oVM5wn6AN0w2vwUgA0O24oLzGQpp1XKI6LLQ0. 13. echo($str) — : In ra một hay nhiều xâu <?php echo “Hello”; //Kết quả: Hello ?> 14. explode($separator, $str, $limit) — Tách chuỗi $str thành mảng bởi chuỗi $separator <?php $str = "Hello world. It's a beautiful day."; print_r (explode(" ",$str)); //Kết quả: Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. ) ?> 15. fprintf — Viết một định dạng chuỗi ra một dòng <?php $number = 9; $str = "Beijing"; $file = fopen("test.txt","w"); echo fprintf($file,"There are %u million bicycles in %s.",$number,$str);//40 //Ghi nội dung “There are 9 million bicycles in Beijing.” Vào file test.txt ?> 16. get_html_translation_table — xem danh sách các giá trị HTML entities <?php print_r (get_html_translation_table()); // Array ( ["] => " [&] => & [<] => < [>] => > ) ?> by Grimes 3 17. hebrev — Chuyển đổi văn ban logic khó hiểu sang văn ban trưc quan <?php echo hebrev(" "� ���� ����� );//âñùúä ïåùç á ?> 18. hebrevc — Chuyển đổi văn ban logic khó hiểu sang văn ban trưc quan với sự chuyển đổi dòng mới <?php echo hebrevc(" � ���� �����\n "� ���� ����� ); //âñùúä ïåùç á //âñùúä ïåùç á ?> 19. hex2bin — Decodes a hexadecimally encoded binary string <?php echo hex2bin("48656c6c6f20576f726c6421");//Hello World! ?> 20. html_entity_decode — chuyển đổi các giá trị HTML entities được gọi bởi hàm htmlentities($str) về giá trị ban đầu <?php $str = "&lt;&copy; W3S&ccedil;h&deg;&deg;&brvbar;&sect;&gt;"; echo html_entity_decode($str); ?> Kết quả: <!DOCTYPE html> <html> <body> < W3S h >� � ���� </body> </html> 21. htmlentities — chuyển đổi các ký tự sang giá trị HTML entities <?php $str = "< W3S h >"� � ���� ; echo htmlentities($str); ?> Kết quả: <!DOCTYPE html> <html> <body> &lt;&copy; W3S&ccedil;h&deg;&deg;&brvbar;&sect;&gt; </body> </html> 22. htmlspecialchars_decode — chuyển đổi các giá trị HTML entities được gọi bởi hàm htmlspecialchars () về giá trị ban đầu <?php $str = "This is some &lt;b&gt;bold&lt;/b&gt; text."; echo htmlspecialchars_decode($str); ?> Kết quả: <!DOCTYPE html> <html> <body> by Grimes 4 This is some <b>bold</b> text. </body> </html> 23. htmlspecialchars — chuyển đổi các ký tự được quy định trước sang giá trị HTML entities <?php $str = "This is some <b>bold</b> text."; echo htmlspecialchars($str); ?> Kết quả: <!DOCTYPE html> <html> <body> This is some &lt;b&gt;bold&lt;/b&gt; text. </body> </html> 24. implode($separator, $array) — Nối các phần tử mảng $array để tạo thành chuỗi, kí tự ngăn cách là $separator. <?php $arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr); //Kết quả: Hello World! Beautiful Day! ?> 25. join($separator, $array) — Tương tự implode <?php $arr = array('Hello','World!','Beautiful','Day!'); echo join(" ",$arr); //Kết quả: Hello World! Beautiful Day! ?> 26. lcfirst($str) — Make a string's first character lowercase <?php echo lcfirst("Hello world!"); //Kết quả: hello world! ?> 27. levenshtein — Tính khoảng cách giữa hai xâu <?php echo levenshtein("Hello World","ello World");//1 echo "<br>"; echo levenshtein("Hello World","ello World",10,20,30);//30 ?> 28. localeconv — Lấy ra thông tin quy cách số <?php setlocale(LC_ALL,"US"); $locale_info = localeconv(); print_r($locale_info); //Array ( [decimal_point] => . [thousands_sep] => , [int_curr_symbol] => USD [currency_symbol] => $ [mon_decimal_point] => . [mon_thousands_sep] => , [positive_sign] => [negative_sign] => - [int_frac_digits] => 2 [frac_digits] => 2 [p_cs_precedes] => 1 [p_sep_by_space] => 0 [n_cs_precedes] => 1 [n_sep_by_space] => 0 [p_sign_posn] => 3 [n_sign_posn] => 0 [grouping] => Array ( [0] => 3 ) [mon_grouping] => Array ( [0] => 3 ) ) ?> 29. ltrim — Loại bỏ các ký tự ở đầu chuỗi, mặc định loại bỏ tất cả khoảng trắng ở đầu chuỗi. <?php by Grimes 5 $str = "Hello World!"; echo $str . "<br>"; echo ltrim($str,"Hello"); //Hello World! //World! ?> 30. md5_file — Mã hóa file dạng md5 <?php $filename = "test.txt"; $md5file = md5_file($filename); echo $md5file; // d41d8cd98f00b204e9800998ecf8427e ?> 31. md5 — Mã hóa chuỗi dạng md5 <?php $str = "Hello"; echo md5($str); //8b1a9953c4611296a827abf8c47804d7 ?> 32. metaphone — Tính siêu khoá âm thanh của 1 chuỗi <?php echo metaphone("World");//WRLT ?> 33. money_format — Định dạng 1 số như 1 chuỗi tiền tệ <?php $number = 1234.56; setlocale(LC_MONETARY,"en_US"); echo money_format("The price is %i", $number);// The price is USD 1,234.56 ?> 34. nl_langinfo — Ngôn ngữ truy vấn và biên tập thông tin 35. nl2br — Chèn 1 dòng ngắt HTML trước tất cả những dòng mới trong 1 chuỗi <?php echo nl2br("One line.\nAnother line."); /* One line. Another line. */ ?> 36. number_format($number, $decimals, $decimalpoint, $separator) — Định dạng số $number với hàng nghìn chữ số <?php echo number_format("1000000")."<br>"; //1,000,000 echo number_format("1000000",2)."<br>"; //1,000,000.00 echo number_format("1000000",2,",",".");//1.000.000,00 ?> 37. ord — Trả lại giá trị ASCII của kí tự <?php echo ord("h")."<br>";//104 by Grimes 6 echo ord("hello")."<br>";//104 ?> 38. parse_str — Phân tách chuỗi thành các biến số <?php parse_str("name=Peter&age=43"); echo $name."<br>";//Peter echo $age;//43 ?> 39. print — In ra 1 chuỗi <?php print "Hello"; // Hello ?> 40. printf — In ra 1 chuỗi được định dạng <?php $number = 9; $str = "Beijing"; printf("There are %u million bicycles in %s.",$number,$str);// There are 9 million bicycles in Beijing. ?> 41. quoted_printable_decode — Chuyển 1 chuỗi có thể in trích dẫn sang 1 chuỗi 8 bit.(Giải mã) <?php $str = "Hello=0Aworld."; echo quoted_printable_decode($str);// Hello world. ?> 42. quoted_printable_encode — Chuyển 1 chuỗi có thể in trích dẫn sang 1 chuỗi 8 bit. 43. quotemeta — Trích dẫn kí tự đặc biệt <?php $str = "Hello world. (can you hear me?)"; echo quotemeta($str);// Hello world\. \(can you hear me\?\) ?> 44. rtrim — Loại bỏ các ký tự ở cuối chuỗi, mặc định loại bỏ tất cả khoảng trắng ở cuối chuỗi. <?php $str = "Hello World!"; echo $str . "<br>"; echo rtrim($str,"World!"); ?> 45. setlocale — Thiết lập vị trí thông tin <?php echo setlocale(LC_ALL,"US");//English_United States.1252 echo "<br>"; echo setlocale(LC_ALL,NULL); //English_United States.1252 ?> 46. sha1_file — Mã hoá sha1 1 file <?php $filename = "test.txt"; $sha1file = sha1_file($filename); echo $sha1file; //aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d by Grimes 7 ?> 47. sha1 — Mã hóa chuỗi dạng sha1 <?php $str = "Hello"; echo sha1($str);// f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 ?> 48. similar_text — Cho số kí tự giống nhau của 2 chuỗi <?php echo similar_text("Hello World","Hello Peter"); //7 ?> 49. soundex — Tính khoá chỉ âm của 1 chuỗi <?php $str = "Hello"; echo soundex($str);//H400 ?> 50. sprintf — Trả lại 1 chuỗi được định dạng <?php $number = 9; $str = "Beijing"; $txt = sprintf("There are %u million bicycles in %s.",$number,$str); echo $txt;// There are 9 million bicycles in Beijing. ?> 51. sscanf — Phân tách chuỗi theo 1 định dạng <?php $str = "age:30 weight:60kg"; sscanf($str,"age:%d weight:%dkg",$age,$weight); // show types and values var_dump($age,$weight);// int(30) int(60) ?> 52. str_getcsv(string,separator,enclosure,escape) — Parse a CSV string into an array 53. str_ireplace — Thay thế ngược lại khi xâu đã bị thay thế bằng str_replace(). <?php echo str_ireplace("WORLD","Peter","Hello world!");// Hello Peter! ?> 54. str_pad — Tăng độ dài của chuỗi với các ký tự mới <?php $str = "Hello World"; echo str_pad($str,20,".");// Hello World ?> 55. str_repeat — cho phép lặp lại chuỗi $str theo $n lần <?php echo str_repeat(".",13);// …………. ?> 56. str_replace — tìm kiếm và thay thế trong chuỗi: <?php by Grimes 8 echo str_replace("world","Peter","Hello world!");// Hello Peter! ?> 57. str_rot13 — Thực hiện mã hoá theo rot 13 trên 1 xâu <?php echo str_rot13("Hello World");//Uryyb Jbeyq echo "<br>"; echo str_rot13("Uryyb Jbeyq");//Hello World ?> 58. str_shuffle — sắp xếp ngẫu nhiên thứ tự các ký tự trong chuỗi <?php echo str_shuffle("Hello World");//leWlHdloor ?> 59. str_split — Cắt chuỗi thành các phần tử của mảng <?php print_r(str_split("Hello"));//Array ( [0] => H [1] => e [2] => l [3] => l [4] => o ) ?> 60. str_word_count — Đếm tổng số từ có trong chuỗi <?php echo str_word_count("Hello world!");//2 ?> 61. strcasecmp — Binary safe case-insensitive string comparison <?php echo strcasecmp("Hello world!","HELLO WORLD!");//0 ?> 62. strchr — Alias of strstr <?php echo strchr("Hello world!","world");//world! ? 63. strcmp — so sánh hai chuỗi <?php echo strcmp("Hello world!","Hello world!");//0 ?> 64. strcoll — Locale based string comparison <?php setlocale (LC_COLLATE, 'NL'); echo strcoll("Hello World!","Hello World!");//0 echo "<br>"; setlocale (LC_COLLATE, 'en_US'); echo strcoll("Hello World!","Hello World!");//0 ?> 65. strcspn — Find length of initial segment not matching mask <?php echo strcspn("Hello world!","w");//6 ?> by Grimes 9 66. strip_tags — để loại bỏ các thẻ HTML có trong chuỗi <?php echo strip_tags("Hello <b>world!</b>");//Hello world! ?> 67. stripcslashes — hiển thị chuỗi không có các ký tự gạch chéo được tạo bởi hàm addcslashes <?php echo stripcslashes("Hello \World!");// Hello World! ?> 68. stripos — Find the position of the first occurrence of a case-insensitive substring in a string <?php echo stripos("I love php, I love php too!","PHP");//7 ?> 69. stripslashes — hiển thị chuỗi không có các ký tự gạch chéo được tạo bởi hàm addslashes <?php echo stripslashes("Who\'s Peter Griffin?");// Who's Peter Griffin? ?> 70. stristr — Case-insensitive strstr <?php echo stristr("Hello world!","WORLD");//world! ?> 71.strlen($str) — Đếm tổng số ký tự có trong chuỗi <?php echo strlen("Hello");//5 ?> 72. strnatcasecmp — Case insensitive string comparisons using a "natural order" algorithm <?php echo strnatcasecmp("2Hello world!","10Hello WORLD!");// -1 echo "<br>"; echo strnatcasecmp("10Hello world!","2Hello WORLD!"); // 1 ?> 73. strnatcmp — String comparisons using a "natural order" algorithm <?php echo strnatcmp("2Hello world!","10Hello world!");// -1 echo "<br>"; echo strnatcmp("10Hello world!","2Hello world!");// 1 ?> 74. strncasecmp — Binary safe case-insensitive string comparison of the first n characters <?php echo strncasecmp("Hello world!","hello earth!",6);//0 ?> 75.strncmp — Binary safe string comparison of the first n characters by Grimes 10 [...]...< ?php echo strncmp("Hello world!","Hello earth!",6);//0 ?> 76 strpbrk — Search a string for any of a set of characters < ?php echo strpbrk("Hello world!","oe");//ello world! ?> 77 strpos — Tìm vì trí xuất hiện đầu tiên của $chuoi_tim trong chuỗi $str < ?php echo strpos("I love php, I love php too!", "php" );//7 ?> 78 strrchr — Find the last occurrence of a character in a string < ?php echo strrchr("Hello... strrev — Reverse a string < ?php echo strrev("Hello World!");// !dlroW olleH ?> 80 strripos — Find the position of the last occurrence of a case-insensitive substring in a string < ?php echo strripos("I love php, I love php too!", "PHP" );//19 ?> 81 strrpos — Find the position of the last occurrence of a substring in a string < ?php echo strrpos("I love php, I love php too!", "php" );//19 ?> 82 strspn — Finds... lần xuất hiện chuỗi con < ?php echo substr_count("Hello world The world is nice","world");//2 ?> 90 substr_replace — thay thế một đoạn văn bản trong phạm vi một đoạn của xâu kí tự < ?php echo substr_replace("Hello","world",0);// world ?> 91 substr — tách chuỗi con từ một chuỗi < ?php echo substr("Hello world",6);// world ?> 92 trim — Loại bỏ những khoảng trắng ở đầu và cuối của chuỗi < ?php $str = "Hello... initial segment of a string consisting entirely of characters contained within a given mask < ?php echo strspn("Hello world!","kHlleo");//5 ?> 83 strstr — Tách ra một chuỗi con từ vị trí đầu tiên của chuỗi cho trước cho đến cuối chuỗi < ?php echo strstr("Hello world!","world");//world! ?> 84 strtok — Tokenize string < ?php $string = "Hello world Beautiful day today."; $token = strtok($string, " "); while ($token... chữ thường < ?php echo strtolowper("HELLO WORLD!");// hello world! ?> 86 strtoupper — Chuyển tất cả ký tự sang chữ in hoa < ?php echo strtoupper("Hello WORLD!");// HELLO WORLD! ?> 87 strtr — chuyển đổi kí tự theo một qui tắc nào đó < ?php echo strtr("Hilla Warld","ia","eo");//Hello World ?> 88 substr_compare — Binary safe comparison of two strings from an offset, up to length characters < ?php echo substr_compare("Hello... trim($str,"Hed!"); /* 12 by Grimes Hello World! llo Worl */ ?> 93 ucfirst — Viết hoa kí tự đầu tiên của chuỗi < ?php echo ucfirst("hello world!");// Hello world! ?> 94 ucwords — Viết hoa kí tự đầu tiên của mỗi từ < ?php echo ucwords("hello world");//Hello World ?> 95 vfprintf — Write a formatted string to a stream < ?php $number = 9; $str = "Beijing"; $file = fopen("test.txt","w"); echo vfprintf($file,"There are... test.txt nội dung “There are 9 million bicycles in Beijing.” ?> 96 vprintf — Output a formatted string < ?php $number = 9; $str = "Beijing"; vprintf("There are %u million bicycles in %s.",array($number,$str)); //There are 9 million bicycles in Beijing ?> 97 vsprintf — quai lại một xâu định dạng < ?php $number = 9; $str = "Beijing"; $txt = vsprintf("There are %u million bicycles in %s.",array($number,$str));... vsprintf("There are %u million bicycles in %s.",array($number,$str)); echo $txt;// There are 9 million bicycles in Beijing ?> 98 wordwrap — chia xâu kí tự ra thành các sâu con nhờ một kí tự xác định < ?php $str = "An example of a long word is: Supercalifragulistic"; echo wordwrap($str,15,"\n"); /* An example of a long word is: Supercalifragulistic */ ?> 13 by Grimes

Ngày đăng: 19/11/2014, 17:33

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan