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
Trang 1PHP – 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
// Kết quả: \n\e\t
// 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
?>
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, ,".")
//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>";
//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 `";
?>
Trang 29 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!";
?>
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 == )
{
echo "Standard DES: ".crypt('something','st')."\n<br>";
}
else
{
echo "Standard DES not supported.\n<br>";
}
// 4 character salt
if CRYPT_EXT_DES == )
{
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 == )
{
echo "MD5: ".crypt('something', $ $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 == )
{
echo "Blowfish: ".crypt('something', $2a$09$anexamplestringforsalt$ )."\n<br>";
}
else
{
echo "Blowfish DES not supported.\n<br>";
Trang 3// 16 character salt starting with $5$ The default number of rounds is 5000
if CRYPT_SHA256 == )
{
echo "SHA-256: ".crypt('something', $ $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 == )
{
}
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 ( [ ] => Hello [ ] => world [ ] => 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
$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 ( ["] => " [&] => & [<] => < [>] => > )
?>
Trang 417 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 "<© W3Sçh°°¦§>";
?>
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� � ����>";
?>
Kết quả:
<!DOCTYPE html>
<html>
<body>
<© W3Sçh°°¦§>
</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 <b>bold</b> text.";
?>
Kết quả:
<!DOCTYPE html>
<html>
<body>
Trang 5This 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.";
?>
Kết quả:
<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> 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
Trang 6$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)
?>
31 md5 — Mã hóa chuỗi dạng md5
<?php
$str "Hello";
?>
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")
?>
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", )."<br>"; //1,000,000.00
echo number_format("1000000", ,",",".");//1.000.000,00
?>
37 ord — Trả lại giá trị ASCII của kí tự
<?php
echo ord("h")."<br>";//104
Trang 7echo 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
?>
39 print — In ra 1 chuỗi
<?php
print "Hello"; // Hello
?>
40 printf — In ra 1 chuỗi được định dạng
<?php
$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.";
?>
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?)";
?>
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>";
?>
46 sha1_file — Mã hoá sha1 1 file
<?php
$filename "test.txt";
$sha1file sha1_file($filename)
Trang 847 sha1 — Mã hóa chuỗi dạng sha1
<?php
$str "Hello";
?>
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";
?>
50 sprintf — Trả lại 1 chuỗi được định dạng
<?php
$str "Beijing";
$txt sprintf("There are %u million bicycles in %s.",$number,$str)
?>
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
Trang 9echo 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
?>
Trang 1066 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!", );//0
?>
75.strncmp — Binary safe string comparison of the first n characters
Trang 11echo strncmp("Hello world!","Hello earth!", );//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 world!","world");//world!
?>
79 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 the length of the 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, " ")
{