PHP 第十一周函数学习记录
htmlentities()
作用
htmlentities() 函数把字符转换为 HTML 实体。
用法
htmlentities(string,flags,character-set,double_encode)
案例
<?php
$str = "Jane & 'Tarzan'";
echo htmlentities($str, ENT_COMPAT);
echo "<br>";
echo htmlentities($str, ENT_QUOTES);
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); convert any quotes
?>
结果
// 上面代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html>
<html>
<body>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'
</body>
</html>
// 上面代码的浏览器输出如下:
Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'
htmlspecialchars_decode()
作用
htmlspecialchars_decode() 函数把一些预定义的 HTML 实体转换为字符。
会被解码的 HTML 实体是:
& 解码成 & (和号)
" 解码成 " (双引号)
' 解码成 ' (单引号)
< 解码成 < (小于)
> 解码成 > (大于)
用法
htmlentities(string,flags,character-set,double_encode)
案例
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars_decode($str, ENT_COMPAT); // 默认,仅解码双引号
echo "<br>";
echo htmlspecialchars_decode($str, ENT_QUOTES); // 解码双引号和单引号
echo "<br>";
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // 不解码任何引号
?>
结果
// 上面代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html>
<html>
<body>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'
</body>
</html>
// 上面代码的浏览器输出如下:
Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'
htmlspecialchars()
作用
htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。
预定义的字符是:
& (和号)成为 &
" (双引号)成为 "
' (单引号)成为 '
< (小于)成为 <
> (大于)成为 >
用法
htmlentities(string,flags,character-set,double_encode)
案例
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT); // 默认,仅编码双引号
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); // 编码双引号和单引号
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // 不编码任何引号
?>
结果
// 上面代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html>
<html>
<body>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'
</body>
</html>
// 上面代码的浏览器输出如下:
Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'
implode()
作用
implode() 函数返回一个由数组元素组合成的字符串。
用法
implode(separator,array)
案例
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
结果
Hello World! Beautiful Day!
join()
作用
join() 函数返回一个由数组元素组合成的字符串。
join() 函数是 implode() 函数的别名。
用法
join(separator,array)
案例
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo join(" ",$arr);
?>
结果
Hello World! Beautiful Day!
lcfirst()
作用
lcfirst() 函数把字符串中的首字符转换为小写。
相关函数
- ucfirst() - 把字符串中的首字符转换为大写。
- ucwords() - 把字符串中每个单词的首字符转换为大写。
- strtoupper() - 把字符串转换为大写。
- strtolower() - 把字符串转换为小写。
用法
lcfirst(string)
案例
<?php
echo lcfirst("Hello world!");
?>
结果
hello world!
levenshtein()
作用
函数返回两个字符串之间的 Levenshtein 距离。
Levenshtein 距离,又称编辑距离,指的是两个字符串之间,由一个字符串转换成另一个字符串所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。
相关函数
- ucfirst() - 把字符串中的首字符转换为大写。
- ucwords() - 把字符串中每个单词的首字符转换为大写。
- strtoupper() - 把字符串转换为大写。
- strtolower() - 把字符串转换为小写。
用法
levenshtein(string1,string2,insert,replace,delete)
string1 必需。要比较的第一个字符串。
string2 必需。要比较的第二个字符串。
insert 可选。插入一个字符的代价。默认是 1。
replace 可选。替换一个字符的代价。默认是 1。
delete 可选。删除一个字符的代价。默认是 1。
案例
<?php
echo levenshtein("Hello World","ello World");
echo "<br>";
echo levenshtein("Hello World","ello World",10,20,30);
?>
结果
1
30
localeconv()
作用
函数返回一个包含本地数字及货币格式信息的数组。
用法
localeconv(string1,string2,insert,replace,delete)
案例
<?php
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
(
)
)
ltrim()
作用
ltrim() 函数移除字符串左侧的空白字符或其他预定义字符。
用法
ltrim(string,charlist)
案例
<?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>
结果
Hello World!
World!
md5()
作用
md5() 函数计算字符串的 MD5 散列。
用法
md5(string,raw)
案例
$str = "Hello";
echo "The string: ".$str."<br>";
echo "TRUE - Raw 16 character binary format: ".md5($str, TRUE)."<br>";
echo "FALSE - 32 character hex number: ".md5($str)."<br>";
结果
The string: Hello
TRUE - Raw 16 character binary format: SÄa¨'«øÄx×
FALSE - 32 character hex number: 8b1a9953c4611296a827abf8c47804d7
md5_file()
作用
md5_file() 函数计算文件的 MD5 散列。
用法
md5_file(file,raw)
案例
<?php
$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;
?>
结果
d41d8cd98f00b204e9800998ecf8427e
metaphone()
作用
metaphone() 函数计算字符串的 metaphone 键。
用法
metaphone(string,length)
注释和提示
metaphone() 函数计算字符串的 metaphone 键。
metaphone 键代表了字符串的英语发音。
metaphone() 函数可用于拼写检查程序。
注释:metaphone() 函数为发音相似的单词创建相同的键。
注释:所生成的 metaphone 键长度可变。
提示:metaphone() 比 soundex() 函数更精确,因为 metaphone() 了解英语发音的基本规则。
案例
<?php
echo metaphone("World");
?>
结果
WRLT
money_format()
作用
money_format() 函数返回格式化为货币字符串的字符串。
用法
money_format(string,number)
案例
<?php
$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("The price is %i", $number);
?>
结果
The price is USD 1,234.56
nl_langinfo()
作用
nl_langinfo() 函数返回指定的本地信息。
用法
nl_langinfo(element)
nl2br()
作用
nl2br() 函数在字符串中的每个新行(\n)之前插入 HTML 换行符(<br> 或 <br />)。
用法
nl2br(string,xhtml)
案例
<?php
echo nl2br("One line.\nAnother line.");
?>
结果
// 上面代码的浏览器输出如下:
One line.
Another line.
// 上面代码的 HTML 输入如下(查看源代码):
One line.<br />
Another line.
number_format()
作用
number_format() 函数通过千位分组来格式化数字。
用法
number_format(number,decimals,decimalpoint,separator)
number 必需。要格式化的数字。如果未设置其他参数,则数字会被格式化为不带小数点且以逗号(,)作为千位分隔符。
decimals 可选。规定多少个小数。如果设置了该参数,则使用点号(.)作为小数点来格式化数字。
decimalpoint 可选。规定用作小数点的字符串。
separator 可选。规定用作千位分隔符的字符串。仅使用该参数的第一个字符。比如 "xxx" 仅输出 "x"。
注释:如果设置了该参数,那么所有其他参数都是必需的。
注意
注释:该函数支持一个、两个或四个参数(不是三个)。
案例
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
结果
1,000,000
1,000,000.00
1.000.000,00
ord()
作用
ord() 函数返回字符串中第一个字符的 ASCII 值。
用法
ord(string)
案例
<?php
echo ord("h")."<br>";
echo ord("hello")."<br>";
?>
结果
104
104
parse_str()
作用
parse_str() 函数把查询字符串解析到变量中。
用法
parse_str(string,array)
案例
<?php
parse_str("name=Peter&age=43",$myArray);
print_r($myArray);
?>
结果
Array (
[name] => Peter
[age] => 43
)
print()
作用
print() 函数输出一个或多个字符串。
用法
print(strings)
注释
注释:print() 函数实际不是一个函数,所以您不必对它使用括号。
提示:print() 函数比 echo() 速度稍慢。
案例
<?php
$str = "Hello world!";
print $str;
?>
结果
Hello world!
printf()
作用
printf() 函数输出格式化的字符串。
用法
printf(format,arg1,arg2,arg++)
注释
arg1、arg2、++ 参数将被插入到主字符串中的百分号(%)符号处。该函数是逐步执行的。在第一个 % 符号处,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。
注释:如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入到 % 符号之后,由数字和 "\$" 组成。请参见实例 2。
提示:相关函数:sprintf()、 vprintf()、 vsprintf()、 fprintf() 和 vfprintf()
案例
<?php
$number = 9;
$str = "Beijing";
printf("There are %u million bicycles in %s.",$number,$str);
?>
结果
There are 9 million bicycles in Beijing.
quoted_printable_decode()
作用
quoted_printable_decode() 对经过 quoted-printable 编码后的字符串进行解码,返回 8 位的 ASCII 字符串
用法
quoted_printable_decode(string)
提示
经过 quoted-printable 编码后的数据与通过邮件传输进行修改的不一样。一个完全 US-ASCII 的文本可进行 quoted-printable 编码,用来确保通过文字翻译或线包网关进行消息传递时数据的完整性。
案例
<?php
$str = "Hello=0Aworld.";
echo quoted_printable_decode($str);
?>
结果
上面代码的浏览器输出如下:
Hello world.
上面代码的 HTML 输出如下(查看源代码):
Hello
world.
quoted_printable_encode()
作用
quoted_printable_encode() 函数把 8 位字符串转换为 quoted-printable 字符串。
用法
quoted_printable_encode(string)
提示
经过 quoted-printable 编码后的数据与通过邮件传输进行修改的不一样。一个完全 US-ASCII 的文本可进行 quoted-printable 编码,用来确保通过文字翻译或线包网关进行消息传递时数据的完整性。
quotemeta()
作用
quotemeta() 函数在字符串中某些预定义的字符前添加反斜杠。
预定义的字符:
- 句号(.)
- 反斜杠(\)
- 加号(+)
- 星号(*)
- 问号(?)
- 方括号([])
- 脱字号(^)
- 美元符号($)
- 圆括号(())
用法
quotemeta(string)
提示
该函数可用于转义拥有特殊意义的字符,比如 SQL 中的 ( )、[ ] 以及 * 。
该函数是二进制安全的。
案例
<?php
$str = "Hello world. (can you hear me?)";
echo quotemeta($str);
?>
结果
Hello world\. \(can you hear me\?\)
rtrim()
作用
rtrim() 函数移除字符串右侧的空白字符或其他预定义字符。
用法
rtrim(string,charlist)
案例
<?php
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
结果
上面代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html>
<html>
<body>
Without rtrim: Hello World! <br>With rtrim: Hello World!
</body>
</html>
setlocale()
作用
setlocale() 函数设置地区信息(地域信息)。
地区信息是针对一个地理区域的语言、货币、时间以及其他信息。
用法
setlocale(constant,location)
注释和提示
注释:setlocale() 函数仅针对当前脚本改变地区信息。
提示:可以通过 setlocale(LC_ALL,NULL) 把地区信息设置为系统默认。
提示:如需获取数字格式信息,请查看 localeconv() 函数。
案例
// 设置地区为 US English,然后再设置回系统默认
<?php
echo setlocale(LC_ALL,"US");
echo "<br>";
echo setlocale(LC_ALL,NULL);
?>
结果
English_United States.1252
English_United States.1252
来源
本作品采用《CC 协议》,转载必须注明作者和本文链接