PHP String 函数
stristr() 函数查找字符串在另一个字符串中第一次出现的位置。
如果成功,则返回字符串的其余部分(从匹配点)。如果没有找到该字符串,则返回 false。
stristr(string,search)
2.rawurldecode()
rawurldecode ― 对已编码的 URL 字符串进行解码
rawurldecode() 不会把加号('+')解码为空格,而 urldecode() 可以。
3.set_time_limit(0);
括号里边的数字是执行时间,如果为零说明永久执行直到程序结束,如果为大于零的数字,则不管程序是否执行完成,到了设定的秒数,程序结束。
4.get_magic_quotes_gpc()
magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post、get、cookie过来的数据增加转义字符“\”
在PHP6中***了magic_quotes_gpc这个选项 $c=(!get_magic_quotes_gpc())?addslashes($c):$c;
5.str_pad()把字符串填充为指定的长度
str_pad(string,length,pad_string,pad_type) str_pad($str,20,".");
6.
(PHP 4 >= 4.0.4, PHP 5)
call_user_func_array ― 调用回调函数,并把一个数组参数作为回调函数的参数
<?php
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
function bar($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2\n";
}
}
// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));
// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>
7.
http_build_query ― 生成 URL-encode 之后的请求字符串
<?php
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');
?>
以上例程会输出:
foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&baz=boom&cow=milk&php=hypertext+processor
8.
PHP SimpleXML 函数
addChild() 函数向指定的 XML 节点添加一个子节点。
该函数返回一个 SimpleXMLElement 对象,这个对象表示添加到 XML 节点的子元素。
class SimpleXMLElement { string addChild(name,value,ns) }
参数 | 描述 |
---|---|
name | 必需。规定子元素的名称。 |
value | 必需。规定子元素的值。 |
ns | 可选。规定子元素的命名空间。 |
XML 文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
PHP 代码:
<?php $xml = simplexml_load_file("test.xml"); $xml->body[0]->addChild("date", "2008-08-08"); foreach ($xml->body->children() as $child) { echo "Child node: " . $child; } ?>
输出:
Child node: 2008-08-08
9.filter_input()
filter_input() 函数从脚本外部获取输入,并进行过滤。
本函数用于对来自非安全来源的变量进行验证,比如用户的输入。
本函数可从各种来源获取输入:
INPUT_GET
INPUT_POST
INPUT_COOKIE
INPUT_ENV
INPUT_SERVER
INPUT_SESSION (Not yet implemented)
INPUT_REQUEST (Not yet implemented)
如果成功,则返回被过滤的数据,如果失败,则返回 false,如果 variable 参数未设置,则返回 NULL。
filter_input(input_type, variable, filter, options)
<?php if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)) { echo "E-Mail is not valid"; } else { echo "E-Mail is valid"; } ?>