php string 函数

来源:互联网 发布:经典网络武侠小说 编辑:程序博客网 时间:2024/05/11 12:34


1。  addslashes() 函数在指定的预定义字符前添加反斜杠。

这些预定义字符是:

  • 单引号 (')
  • 双引号 (")
  • 反斜杠 (\)
  • NULL

语法

addslashes(string)
参数描述string

必需。规定要检查的字符串。



注释:默认情况下,PHP 指令 magic_quotes_gpc 为 on,对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。


例子

在本例中,我们要向字符串中的预定义字符添加反斜杠:

<?php$str = "Who's John Adams?";echo $str . " This is not safe in a database query.<br />";echo addslashes($str) . " This is safe in a database query.";?>

输出:

Who's John Adams? This is not safe in a database query.Who\'s John Adams? This is safe in a database query.



2. chr() 函数从指定的 ASCII 值返回字符。

chr(ascii)

例子

<?phpecho chr(52);echo chr(052);echo chr(0x52);?>

输出:

4*R




3.htmlentitles()

htmlentities() 函数把字符转换为 HTML 实体。

语法

htmlentities(string,quotestyle,character-set)
参数描述string必需。规定要转换的字符串。quotestyle

可选。规定如何编码单引号和双引号。

  • ENT_COMPAT - 默认。仅编码双引号。
  • ENT_QUOTES - 编码双引号和单引号。
  • ENT_NOQUOTES - 不编码任何引号。
character-set

可选。字符串值,规定要使用的字符集。

  • ISO-8859-1 - 默认。西欧。
  • ISO-8859-15 - 西欧(增加 Euro 符号以及法语、芬兰语字母)。
  • UTF-8 - ASCII 兼容多字节 8 比特 Unicode
  • cp866 - DOS 专用 Cyrillic 字符集
  • cp1251 - Windows 专用 Cyrillic 字符集
  • cp1252 - Windows 专用西欧字符集
  • KOI8-R - 俄语
  • GB2312 - 简体中文,国家标准字符集
  • BIG5 - 繁体中文
  • BIG5-HKSCS - Big5 香港扩展
  • Shift_JIS - 日语
  • EUC-JP - 日语

提示和注释

提示:无法被识别的字符集将被忽略,并由 ISO-8859-1 代替。

例子

<html><body><?php$str = "John & 'Adams'";echo htmlentities($str, ENT_COMPAT);echo "<br />";echo htmlentities($str, ENT_QUOTES);echo "<br />";echo htmlentities($str, ENT_NOQUOTES);?></body></html>

浏览器输出:

John & 'Adams'John & 'Adams'John & 'Adams'

如果在浏览器中查看源代码,会看到这些 HTML:

<html><body>John &amp; 'Adams'<br />John &amp; &#039;Adams&#039;<br />John &amp; 'Adams'</body></html>




4.explode()

explode(separator,string,limit)
参数描述separator必需。规定在哪里分割字符串。string必需。要分割的字符串。limit可选。规定所返回的数组元素的最大数目。
explode() 函数把字符串分割为数组。

在本例中,我们将把字符串分割为数组:

<?php$str = "Hello world. It's a beautiful day.";print_r (explode(" ",$str));?>

输出:

Array([0] => Hello[1] => world.[2] => It's[3] => a[4] => beautiful[5] => day.)



5.implode()


implode() 函数把数组元素组合为一个字符串。
implode(separator,array)
参数描述separator可选。规定数组元素之间放置的内容。默认是 ""(空字符串)。array必需。要结合为字符串的数组。



6. md5() 计算字符串的 MD5 散列。

7.nl2br()
nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (<br />)。

<?phpecho nl2br("One line.\nAnother line.");?>

输出:

One line.Another line.

HTML 代码:

One line.<br />Another line.


8. ord() 返回字符串第一个字符的 ASCII 值。

9.stripslashes()
stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。


例子

<?phpecho stripslashes("Who\'s John Adams?");?>

输出:

Who's John Adams?


10.mysql_real_escape_string()

mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符。

下列字符受影响:

  • \x00
  • \n
  • \r
  • \
  • '
  • "
  • \x1a

如果成功,则该函数返回被转义的字符串。如果失败,则返回 false。



11.str_replace()

str_replace() 函数使用一个字符串替换字符串中的另一些字符。


str_replace(find,replace,string,count)
参数描述find必需。规定要查找的值。replace必需。规定替换 find 中的值的值。string必需。规定被搜索的字符串。count可选。一个变量,对替换数进行计数。
<?phpecho str_replace("world","John","Hello world!");?>

输出:

Hello John!



12.
array_keys(array,value)

array_keys() 函数返回包含数组中所有键名的一个新数组。

如果提供了第二个参数,则只返回键值为该值的键名。

如果 strict 参数指定为 true,则 PHP 会使用全等比较 (===) 来严格检查键值的数据类型































0 0
原创粉丝点击