php string 函数
来源:互联网 发布:经典网络武侠小说 编辑:程序博客网 时间:2024/05/11 12:34
1。 addslashes() 函数在指定的预定义字符前添加反斜杠。
这些预定义字符是:
- 单引号 (')
- 双引号 (")
- 反斜杠 (\)
- NULL
语法
addslashes(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可选。规定如何编码单引号和双引号。
character-set
- ENT_COMPAT - 默认。仅编码双引号。
- ENT_QUOTES - 编码双引号和单引号。
- ENT_NOQUOTES - 不编码任何引号。
可选。字符串值,规定要使用的字符集。
- 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 & 'Adams'<br />John & 'Adams'<br />John & '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 />)。<?phpechonl2br("One line.\nAnother line.")
;?>输出:
One line.Another line.HTML 代码:
One line.<br />Another line.
8. ord() 返回字符串第一个字符的 ASCII 值。
9.stripslashes()
stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。例子
<?phpechostripslashes("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)
<?phpecho
str_replace("world","John","Hello world!")
;?>输出:
Hello John!
12.
array_keys(array,value)
array_keys() 函数返回包含数组中所有键名的一个新数组。
如果提供了第二个参数,则只返回键值为该值的键名。
如果 strict 参数指定为 true,则 PHP 会使用全等比较 (===) 来严格检查键值的数据类型 0 0
- PHP常用String函数
- PHP String 函数
- php string 函数
- PHP String 函数
- php string 函数
- PHP String 函数
- PHP String字符串函数
- PHP 5 String 函数
- PHP中的字符串函数(String Functions)
- PHP中的字符串函数(String Functions)
- PHP 常用字符串内部函数 string functions
- PHP中String字符串处理函数完整版
- PHP String字符串类函数str_replace
- php基础之string常见函数
- PHP string
- PHP----String
- PHP string
- string.php
- CMT catalog 的下载与更新
- PageRank的模拟计算
- 黑马程序员——OC核心语法3(分类、SEL、类本质等)
- A picture figure out relationship between data style and managment tools
- 全新安装Mac OSX 开发者环境 同时使用homebrew搭建 PHP,Nginx ,MySQL,Redis,Memcache ... ... (LNMP开发环境)
- php string 函数
- python天天进步(1)--sys.argv[]用法
- Linux进程基础与信号基础
- AndroidManifest.xml文件解析
- SQLite3--多表查询
- 黑马程序员--网络编程之TCP
- 自定义组合控件.txt
- 设计模式之策略模式
- 设计模式之工厂方法模式(Factory Method)