编程爱好者之家
1.ini_set()
设置php系统的参数
ini_set("memory_limit", "32M"); // 设置程序最大内存为32M(大文件的处理)2.list()
— 把数组中的值赋给一些变量
list() 仅能用于数字索引的数组并假定数字索引从 0 开始。
$info = array('coffee', 'brown', 'caffeine');
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n"; //coffee is brown and caffeine makes it special.3.each
— 返回数组中当前的键/值对并将数组指针向前移动一步
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo "$key => $val\n";
}以上例程会输出:
a => apple
b => banana
c => cranberry
在执行 each()之后,数组指针将停留在数组中的下一个单元或者当碰到数组结尾时停留在最后一个单元。如果要再用 each 遍历数组,必须使用 reset()。
4.reset()
— 将数组的内部指针指向第一个单元
$array = array('step one', 'step two', 'step three', 'step four');
// by default, the pointer is on the first element
echo current($array) . "<br />\n"; // "step one"
// skip two steps
next($array);
next($array);
echo current($array) . "<br />\n"; // "step three"
// reset pointer, start again on step one
reset($array);
echo current($array) . "<br />\n"; // "step one"5.substr()
— 返回字符串的子串
说明
string substr ( string $string , int $start [, int $length ] )
返回字符串 string 由 start 和 length 参数指定的子字符串。
echo substr('abcdef', 1); // bcdef
echo substr('abcdef', 1, 3); // bcd
echo substr('abcdef', 0, 4); // abcd
echo substr('abcdef', 0, 8); // abcdef
echo substr('abcdef', -1, 1); // f
echo substr('abcdef', 0, -1); //abcde
// 访问字符串中的单个字符
// 也可以使用中括号
$string = 'abcdef';
echo $string[0]; // a
echo $string[3]; // d
echo $string[strlen($string)-1]; // f6.str_pad()
— 使用另一个字符串填充字符串为指定长度
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
该函数返回 input 被从左端、右端或者同时两端被填充到制定长度后的结果。如果可选的 pad_string 参数没有被指定,input 将被空格字符填充,否则它将被 pad_string 填充到指定长度。
input
输入字符串。
pad_length
如果 pad_length 的值是负数,小于或者等于输入字符串的长度,不会发生任何填充。
pad_string
Note:
如果填充字符的长度不能被
pad_string整除,那么pad_string可能会被缩短。
pad_type
可选的 pad_type 参数的可能值为 STR_PAD_RIGHT,STR_PAD_LEFT 或 STR_PAD_BOTH。如果没有指定 pad_type,则假定它是 STR_PAD_RIGHT。
$input = "Alien"; echo str_pad($input, 10); // 输出 "Alien " echo str_pad($input, 10, "-=", STR_PAD_LEFT); // 输出 "-=-=-Alien" echo str_pad($input, 10, "_", STR_PAD_BOTH); // 输出 "__Alien___" echo str_pad($input, 6 , "___"); // 输出 "Alien_"
7.strrpos(),
— 计算指定字符串在目标字符串中最后一次出现的位置
说明
int strrpos ( string $haystack , string $needle [, int $offset = 0 ] )
返回字符串 haystack 中 needle 最后一次出现的数字位置。注意 PHP4 中,needle 只能为单个字符。如果 needle 被指定为一个字符串,那么将仅使用第一个字符。
参数
haystack
在此字符串中进行查找。
needle
如果 needle不是一个字符串,它将被转换为整型并被视为字符的顺序值。
offset
或许会查找字符串中任意长度的子字符串。负数值将导致查找在字符串结尾处开始的计数位置处结束。
$foo = "0123456789a123456789b123456789c"; var_dump(strrpos($foo, '7', -5)); // 从尾部第 5 个位置开始查找 // 结果: int(17) var_dump(strrpos($foo, '7', 20)); // 从第 20 个位置开始查找 // 结果: int(27) var_dump(strrpos($foo, '7', 28)); // 结果: bool(false)
8.strpos()
— 查找字符串首次出现的位置
9.array_merge()
---合并一个或多个数组
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
//以上例程会输出:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)10. scandir()
----列出指定路径中的文件和目录(所有)
scandir() 函数返回一个数组,其中包含指定路径中的文件和目录。
11.eval()
--把字符串作为PHP代码执行
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";以上例程会输出:
This is a $string with my $name in it.
This is a cup with my coffee in it.
12 strrchr ()
— 查找指定字符在字符串中的最后一次出现
// 获取 $PATH 中不含磁盘符号的目录 $dir = substr(strrchr($PATH, ":"), 1); // 获取最后一行内容 $text = "Line 1\nLine 2\nLine 3"; $last = substr(strrchr($text, 10), 1 );
13 uniqid()
— 生成一个唯一ID
echo uniqid(); //5472acd065d4e
14.range()
--建立一个包含指定范围单元的数组
print_r(range(1,3)); //Array ( [0] => 1 [1] => 2 [2] => 3 )
15.in_array()
— 检查数组中是否存在某个值
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}16.array_slice()
array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )
array_slice() 返回根据 offset 和 length 参数所指定的 array数组中的一段序列。
$input = array( "a" , "b" , "c" , "d" , "e" ); $output = array_slice ( $input , 2 ); // returns "c", "d", and "e" $output = array_slice ( $input , - 2 , 1 ); // returns "d" $output = array_slice ( $input , 0 , 3 ); // returns "a", "b", and "c" // note the differences in the array keys print_r ( array_slice ( $input , 2 , - 1 )); print_r ( array_slice ( $input , 2 , - 1 , true ));