php常用函数

  1. realpath()

    返回文件的真实路径,相当于file_exits 的功能,但是返回值是不同的。

  2. gmdate()

    返回格林尼治的时间如果返回东八区的时间 gmdate(‘D,d M Y H:i:s’,time()+3600*8)

  3. preg_repalce()

    替换字符串:


<?php
$patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/',
                   '/^\s*{(\w+)}\s*=/');
$replace = array ('\3/\4/\1\2', '$\1 =');
echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27');
?>

php里面的文件读写 :

  1. file_put_contents()

<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>
$file = fopen($local_file, "r");
    while(!feof($file))
    {
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));
        // flush the content to the browser
        flush();
        // sleep one second
        sleep(1);
    }
    fclose($file);
  1. list()

    分配数组里面对象到变量里面

$info = array('coffee', 'brown', 'caffeine');

// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";

// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";

// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";

// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL

//另外一种用法
list($a, list($b, $c)) = array(1, array(2, 3));

var_dump($a, $b, $c);


  1. explode

    分割字符串

// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
  1. is_array()

    判断是不是一个数组

in_array($key,$group)判断 是不是在数组里面

php里面对数组进行便利:
foreach (self::$aliases AS $key => $val)

注册,加载文件
spl_autoload_register(array($this, ‘loader’));

bool is_dir ( string $filename )
判断给出的文件是否是文件夹

bool is_file ( string $filename )
判断给出的文件是不是一个文件

mixed array_rand ( array $array [, int $num = 1 ] )

array
The input array.
num
Specifies how many entries should be picked.

strstr ()
发现第一个字符在字符串中

$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com

$user = strstr($email, '@', true); // As of PHP 5.3.0
echo $user; // prints name


ob_start()

<?php

function callback($buffer)
{
  // replace all the apples with oranges
  return (str_replace("apples", "oranges", $buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush();

?>

dirname($filename)
返回path的父亲目录,如果path中没有斜线则返回一个点(‘.’)
在windows之中斜线(/)和反斜线()都可以用作目录分隔符。其他环境下面是(/)

<?php
echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc
echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or \ on Windows)
echo "3) " . dirname("."); // 3) .
?>

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
用于发送原生报头
replace 表明是否可以用后面的替换前面的相同的头。默认情况下会替换。如果传入Flase,就可以强制相同的头信息保存。
eader(‘WWW-Authenticate: Negotiate’);
header(‘WWW-Authenticate: NTLM’, false);
请注意 header() 必须在任何实际输出之前调用,不管是普通的 HTML 标签,还是文件或 PHP 输出的空行,空格。
例如错误的用法:

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>

string的头字符串有两种特别的头:

1. 'HTTP/'开头

    1. 将被用来计算将要发送的HTTP状态码:

        * 例如请求服务器上不存在的文件,就希望脚本响应了正确的状态码 

header(“HTTP/1.0 404 Not Found”);

2.    'Location:'开头
        不仅把报文 发送给了且还将返回给浏览器一个REDIRRECT(302)浏览器的状态码,

header(“Location : http://www.baicu.com/“)

范例 ¶
Example #1 下载对话框
如果你想提醒用户去保存你发送的数据,例如保存一个生成的PDF文件。你可以使用» Content-Disposition的报文信息来提供一个推荐的文件名,并且强制浏览器显示一个文件下载的对话框。

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

Example #2 缓存指令
PHP脚本总是会生成一些动态内容,而这些内容是不应该被缓存的,不管是客户端浏览器还是在服务器端和客户端浏览器之间的任何代理。我们可以像这样来强制设置浏览器和各个代理层不缓存数据:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

array_walk_recursive(array &$array,callable $callack[,mixed $userdata=NULL])
对数组中的每个成员递归地调用应用的用户函数。
将用户定义的函数callback 应用到array数组的每个单元。本函数会递归到更深层的数组中去。

callback 典型情况下接受两个参数。array参数的值作为第一个,键名作为第二个。注意:
如果callback需要直接作用于数组中的值,则给callback的第一个参数指定为引用。这样任何对数组单元的改变也将改变原始数组的本身。

userdata:
如果提供了可选的参数userdata,将被作为第三个参数传递给calback.

返回值:
成功时返回TRUE,失败时候返回FALSE.

例子:

<?php
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');

function test_print($item, $key)
{
    echo "$key holds $item\n";
}

array_walk_recursive($fruits, 'test_print');
?>

以上例程会输出:

a holds apple
b holds banana
sour holds lemon

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int$offset = 0 ]]] )
搜索subject与pattern给定的正则表达式的一个匹配.
参数
pattern
要搜索的模式,字符串类型。
subject
输入字符串。
matches
如果提供了参数matches,它将被填充为搜索结果。 $matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推。flags
flags可以被设置为以下标记值:
PREG_OFFSET_CAPTURE
如果传递了这个标记,对于每一个出现的匹配返回时会附加字符串偏移量(相对于目标字符串的)。 注意:这会改变填充到matches参数的数组,使其每个元素成为一个由 第0个元素是匹配到的字符串,第1个元素是该匹配字符串 在目标字符串subject中的偏移量。

例子:

<?php
preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

以上例程会输出:

Array
(
    [0] => Array
        (
            [0] => foobarbaz
            [1] => 0
        )

    [1] => Array
        (
            [0] => foo
            [1] => 0
        )

    [2] => Array
        (
            [0] => bar
            [1] => 3
        )

    [3] => Array
        (
            [0] => baz
            [1] => 6
        )

)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝鲸123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值