PHP 常用函数 - 字符串函数
PHP 常用函数 - 数组函数
PHP 常用函数 - 数学函数
PHP 常用函数 - 目录、文件函数
PHP 常用函数 - 其他常用函数
提示:以下是本篇文章正文内容,下面案例可供参考
$str = "\r\nHello World!\r\n";
echo trim($str); // Hello World!
$str = "Hello World!\n\n";
echo rtrim($str); // Hello World!
echo chop($str); // Hello World!
# chop() 是 rtrim()的别名
$str = "\r\nHello World!";
echo ltrim($str);
echo strtolower("Hello World!"); // hello world!
echo strtoupper("Hello World!"); // HELLO WORLD!
echo ucfirst("hello world"); // Hello world
echo ucwords("hello world"); // Hello World
echo lcfirst("Hello World!"); // hello world!
echo strpos("I love php, I love php too!","php"); // 7
echo stripos("I love php, I love php too!","PHP"); // 7
echo strrpos("I love php, I love php too!","php"); // 7
echo strripos("I love php, I love php too!","PHP"); // 19
strstr()
函数搜索字符串在另一字符串中是否存在,如果是,返回第一次出现该字符串及剩余部分,否则返回 FALSE。
echo strstr("Hello world!","world"); // 输出 world!
echo strchr("Hello world!","world"); // 输出 world!
echo stristr("Hello world!","WORLD"); // world!
echo strrchr("Hello world!","world"); // world!
echo strpbrk("Hello world!","oe"); // ello world!
语法: substr(string,start,length)
echo substr("Hello world",6); // world
substr() 函数,它只针对英文字符,如果要分割的中文文字则需要使用 mb_substr()。
echo mb_substr("菜鸟教程", 0, 2);
// 输出:菜鸟
echo substr_replace("Hello","world",0); // world
echo str_replace("world","Peter","Hello world!"); // Hello Peter!
echo str_ireplace("WORLD","Peter","Hello world!"); // Hello Peter!
echo str_repeat(".",13); // .............
$str = "Hello World";
echo str_pad($str,20,"."); // Hello World.........
echo strrev("Hello World!"); // !dlroW olleH
echo strtr("Hilla Warld","ia","eo"); // Hello World
echo str_shuffle("Hello World"); // lolWo drHle
$str = "Hello world!";
echo chunk_split($str,1,"."); // H.e.l.l.o. .w.o.r.l.d.!.
$string = "Hello world. Beautiful day today.";
$token = strtok($string, " ");
while ($token != false)
{
echo "$token
";
$token = strtok(" ");
}
// 输出:
Hello
world.
Beautiful
day
today.
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"
\n");
// 输出:
An example of a
long word is:
Supercalifragulistic
echo number_format("1000000")."
"; // 1,000,000
echo number_format("1000000",2)."
"; // 1,000,000.00
echo number_format("1000000",2,",","."); // 1.000.000,00
echo dirname("c:/testweb/home.php"); //c:/testweb
语法:strnatcmp(string1,string2);
返回值:
0 - 如果两个字符串相等
<0 - 如果 string1 小于 string2
>0 - 如果 string1 大于 string2
echo strnatcmp("2Hello world!","10Hello world!"); // -1
echo strnatcmp("10Hello world!","2Hello world!"); // 1
语法: strncasecmp(string1,string2,length);
echo strncasecmp("Hello world!","hello earth!",6); // 0
该函数与 strncasecmp() 函数类似,不同的是,通过 strncasecmp() 可以指定每个字符串用于比较的字符数。
语法: strcasecmp(string1,string2);
返回值:
0 - 如果两个字符串相等
<0 - 如果 string1 小于 string2
>0 - 如果 string1 大于 string2
echo strcasecmp("Hello world!","HELLO WORLD!"); // 0
语法:substr_compare(string1,string2,startpos,length,case);
echo substr_compare("Hello world","Hello world",0); // 0
echo strncasecmp("Hello world!","hello earth!",6); // 0
echo strncmp("Hello world!","Hello earth!",6); // 0
echo strcmp("Hello world!","Hello world!"); // 0
setlocale (LC_COLLATE, 'NL');
echo strcoll("Hello World!","Hello World!"); // 0
setlocale (LC_COLLATE, 'en_US');
echo strcoll("Hello World!","Hello World!"); // 0
echo similar_text("Hello World","Hello Peter"); // 7 - 相同字符数量
echo strspn("Hello world!","kHlleo"); // 5
echo strcspn("Hello world!","w"); // 6
echo str_word_count("Hello world!"); // 2
语法: count_chars(string,mode);
mode:可选。规定返回模式。默认是 0。有以下不同的返回模式:
0 - 数组,ASCII 值为键名,出现的次数为键值
1 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数大于 0 的值
2 - 数组,ASCII 值为键名,出现的次数为键值,只列出出现次数等于 0 的值
3 - 字符串,带有所有使用过的不同的字符
4 - 字符串,带有所有未使用过的不同的字符
$str = "Hello World!";
echo count_chars($str,3); // !HWdelor
echo strlen("world"); // 5
中文字符串的处理使用 mb_strlen() 函数。使用 mb_strlen 要开启 mbstring 扩展。
echo substr_count("Hello world. The world is nice","world"); // 2
print_r(str_split("Hello")); // Array ( [0] => H [1] => e [2] => l [3] => l [4] => o )
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr); // Hello World! Beautiful Day!
$str = "www.baidu.com";
print_r(explode(".",$str)); // Array([0] => www [1] => baidu [2] => com)
str_getcsv() 函数解析 CSV 格式字段的字符串,并返回一个包含所读取字段的数组。
语法:str_getcsv(string,separator,enclosure,escape);
string 必需。规定要解析的字符串。
separator 可选。设置字段分界符(只允许一个字符),默认值为逗号( , )。
enclosure 可选。设置字段环绕符(只允许一个字符),默认值为双引号( " )。
escape 可选。设置转义字符(只允许一个字符),默认值为反斜线( \ )。
$str = "<© Helloçh°°¦§>";
echo htmlentities($str);
上面代码的浏览器输出如下:
<© Helloçh°°¦§>
$str = "<© Helloçh°°¦§>;
echo html_entity_decode($str);
上面代码的浏览器输出如下:
<© Hello°°¦§>
$str = "This is some bold text.";
echo htmlspecialchars($str);
上面代码的浏览器输出如下:
This is some bold text.
把预定义的 HTML 实体 "<"(小于)和 ">"(大于)转换为字符:
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars_decode($str);
上面代码的浏览器输出如下:
This is some bold text.
print_r (get_html_translation_table());
// 输出:
Array
(
["] => "
[&] => &
[<] => <
[>] => >
)
$str = addcslashes("Hello World!","W");
echo($str); // Hello \World!
$str = addcslashes("Hello World!","W");
echo($str); // Hello \World!
$str = addslashes('What does "yolo" mean?');
echo($str); // What does \"yolo\" mean?
echo stripslashes("Who\'s Peter Griffin?"); // Who's Peter Griffin?
echo strip_tags("Hello world!"); //
echo nl2br("One line.\nAnother line.");
上面代码的浏览器输出如下:
One line.
Another line.
上面代码的 HTML 输入如下(查看源代码):
One line.
Another line.
$str = "Hello";
echo md5($str); // 8b1a9953c4611296a827abf8c47804d7
$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file; // d41d8cd98f00b204e9800998ecf8427e
$str = "Hello";
echo sha1($str); // f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file; // aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
$str = crc32("Hello World!");
printf("%un",$str); // 472456355
语法: crypt(str,salt); // 实际用法参考官方文档
echo ord("h"); // 104
echo ord("hello"); // 104
echo chr(52) ; // 4
echo chr(052); // *
echo chr(0x52); // R
$str = bin2hex("Hello World!");
echo($str); // 48656c6c6f20576f726c6421
echo hex2bin("48656c6c6f20576f726c6421"); // Hello World!
$number = 9;
$str = "Beijing";
$file = fopen("test.txt","w");
echo vfprintf($file,"There are %u million bicycles in %s.",array($number,$str)); // 40 返回被写的字符串的长度。
# 下面的文本将被写入到文件 "test.txt":There are 9 million bicycles in Beijing.
$number = 9;
$str = "Beijing";
$file = fopen("test.txt","w");
echo fprintf($file,"There are %u million bicycles in %s.",$number,$str); // 40
下面的文本将被写入到文件 "test.txt":There are 9 million bicycles in Beijing.
$number = 9;
$str = "Beijing";
vprintf("There are %u million bicycles in %s.",array($number,$str)); // There are 9 million bicycles in Beijing.
$number = 9;
$str = "Beijing";
$txt = vsprintf("There are %u million bicycles in %s.",array($number,$str));
echo $txt; // There are 9 million bicycles in Beijing.
sscanf() 函数基于格式字符串解析字符串到变量中。
$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)
parse_str("name=Peter&age=43");
echo $name; // Peter
echo $age; // 43
注释:php.ini 文件中的 magic_quotes_gpc 设置影响该函数的输出。如果已启用,那么在 parse_str() 解析之前,变量会被 addslashes() 转换。
print(): 输出字符串
printf(): 输出格式化字符串
echo(): 输出一个或多个字符串
$str = "Hello world! æøå";
echo $str . "
"; // Hello world! æøå
echo convert_cyr_string($str,'w','a'); // Hello world! ¦è¥
$str = ",2&5L;&@=V]R;&0A `";
echo convert_uudecode($str); // Hello world!
$str = "Hello world!";
echo convert_uuencode($str); // ,2&5L;&\@=V]R;&0A `
echo str_rot13("Hello World"); // Uryyb Jbeyq
echo str_rot13("Uryyb Jbeyq"); // Hello World
echo setlocale(LC_ALL,"US"); // English_United States.1252
echo setlocale(LC_ALL,NULL); // English_United States.1252
语法:nl_langinfo(element); 详情参考官方手册。
setlocale(LC_ALL,"US");
$locale_info = localeconv();
print_r($locale_info);
// 输出:
Array
(
[decimal_point] => .
[thousands_sep] =>
[int_curr_symbol] =>
[currency_symbol] =>
[mon_decimal_point] =>
[mon_thousands_sep] =>
[positive_sign] =>
[negative_sign] =>
[int_frac_digits] => 127
[frac_digits] => 127
[p_cs_precedes] => 127
[p_sep_by_space] => 127
[n_cs_precedes] => 127
[n_sep_by_space] => 127
[p_sign_posn] => 127
[n_sign_posn] => 127
[grouping] => Array
(
)
[mon_grouping] => Array
(
)
)
$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("The price is %i", $number); // The price is USD 1,234.56
echo levenshtein("Hello World","ello World"); // 1
echo levenshtein("Hello World","ello World",10,20,30); // 30
$str = "Hello world. (can you hear me?)";
echo quotemeta($str); // Hello world\. \(can you hear me\?\)
$str = "Hello=0Aworld.";
echo quoted_printable_decode($str);
// 上面代码的浏览器输出如下:
Hello world.
// 上面代码的 HTML 输出如下(查看源代码):
Hello
world.
语法: quoted_printable_encode(string); 详情参考官方手册。
echo soundex("Hello"); // H400
echo metaphone("World"); // WRLT
echo hebrev("á çùåï äúùñâ"); // âñùúä ïåùç á
echo hebrevc("á çùåï äúùñâ\ná çùåï äúùñâ");
// 输出:
âñùúä ïåùç á
âñùúä ïåùç á