例子 1. ereg() 例子
<?php
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
echo "$regs[3].$regs[2].$regs[1]";
} else {
echo "Invalid date format: $date";
}
?> 
例子 2. ereg_replace() 例子
<?php
$string = "This is a test";
echo str_replace(" is", " was", $string);
echo ereg_replace("( )is", "\\1was", $string);
echo ereg_replace("(( )is)", "\\2was", $string);
?> 
例子 2. ereg_replace() 例子
<?php
/* 不能产生出期望的结果 */
$num = 4;
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo $string;  /* Output: 'This string has  words.' */
/
* 本例工作正常 */
$num = '4';
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo $string;  /* Output: 'This string has 4 words.' */
?> 
例子 3. 将 URL 替换为超连接
<?php
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"<a href=\"\\0\">\\0</a>", $text);
?> 
例子 4. strstr() example
<?php
$email = 'user@example';
$domain = strstr($email, '@');
echo $domain; // prints @example
?> 
例子 5. dirname() 例子
<?php
$path = "/etc/passwd";
$file = dirname($path); // $file is set to "/etc"
?> 
例子 5. basename() 例子
<?php
$path = "/home/httpd/html/index.php";
$file = basename($path);        // $file is set to "index.php"
$file = basename($path,".php"); // $file is set to "index"
?> 
例子 6. header() 例子 
<?php
header("Location: ample/"); /* 重定向浏览器 */
/* 确保重定向后,后续代码不会被执行 */
exit;
?
>
<?php
// 这样将会直接输出一个 PDF 文件
header('Content-type: application/pdf');
// 这样做就会提示下载 PDF 文件 downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// 这是 original.pdf 的源文件
readfile('original.pdf');
?>
例子 7. explode() 示例
<?php
/
/ 示例 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
// 示例 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?> 
加limit 参数示例
<?php
$str = 'one|two|three|four';
// 正数的 limit
print_r(explode('|', $str, 2));
// 负数的 limit
print_r(explode('|', $str, -1));
?> 
以上示例将输出:
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
例子 1. substr()
<?php
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
// Accessing single characters in a string
// can al
so be achived using "curly braces"
$string = 'abcdef';
echo $string{0};                // a
echo $string{3};                // d
echo $string{strlen($string)-1}; // f
?> 
例子 function_exists  如果成功则返回 TRUE,失败则返回 FALSE
<?php
if (function_exists('imap_open')) {
echo "IMAP functions are available.<br />\n";
} else {
echo "IMAP functions are not available.<br />\n";
}
?>
例子 2. Using stripslashes() on an array
<?php
function stripslashes_deep($value)
{
php实例代码详解$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
// Example
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);
/
/ Output
print_r($array);
?> 
上例将输出:
Array
(
[0] => f'oo
[1] => b'ar
[2] => Array
(
[0] => fo'o
[1] => b'ar
)
)
例子 1. addcslashes() 示例
<?php
$escaped = addcslashes($not_escaped, "\0..\37!@\177..\377");
?> 
当定义 charlist 参数中的字符序列时,你需要确实知道介于你自己设置的开始及结束范围之内的都是些什么字符。
<?php
echo addcslashes('foo[ ]', 'A..z');
// 输出:\f\o\o\[ \]
// 所有大小写字母均被转义
// ... 但 [\]^_` 以及分隔符、换行符、回车符等也一并被转义了。
?> 
例子 1. get_magic_quotes_gpc() example
<?php
echo get_magic_quotes_gpc();        // 1
echo $_POST['lastname'];            // O\'reilly
echo addslashes($_POST['lastname']); // O\\\'reilly
if (!get_magic_quotes_gpc()) {
$lastname = addslashes($_POST['lastname']);
} else {
$lastname = $_POST['lastname'];
}
echo $lastname; // O\'reilly
$sql = "INSERT INTO lastnames (lastname) VALUES ('$lastname')";
?> 
例子 31-2. 在运行时关闭魔术引号
<?php
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}
?> 
例子 1. array_map() 例子
<?php
function cube($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?> 
这使得 $b 成为:
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
例子 2. array_map() - 使用更多的数组
<?php
function show_Spanish($n, $m)
{
return("The number $n is called $m in Spanish");
}
function map_Spanish($n, $m)
{
return(array($n => $m));
}
$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");
$c = array_map("
show_Spanish", $a, $b);
print_r($c);
$d = array_map("map_Spanish", $a , $b);
print_r($d);
?> 
上例将输出:
// printout of $c
Array
(
[0] => The number 1 is called uno in Spanish
[1] => The number 2 is called dos in Spanish
[2] => The number 3 is called tres in Spanish
[3] => The number 4 is called cuatro in Spanish
[4] => The number 5 is called cinco in Spanish
)
// printout of $d
Array
(
[0] => Array
(
[1] => uno
)
[1] => Array
(
[2] => dos
)
[2] => Array
(
[3] => tres
)
[3] => Array
(
[4] => cuatro
)
[4] => Array
(
[5] => cinco
)
)
通常使用了两个或更多数组时,它们的长度应该相同,因为回调函数是平行作用于相应的单元上的。如果数组的长度不同,则最短的一个将被用空的单元扩充。
本函数一个有趣的用法是构造一个数组的数组,这可以很容易的通过用 NULL 作为回调函数名来实现。
例子 3. 建立一个数组的数组
<?php
$a = array(1, 2, 3, 4, 5);
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");
$d = array_map(null, $a, $b, $c);
print_r($d);
?
上例将输出:
Array
(
[0] => Array
(
[0] => 1
[1] => one
[2] => uno
)
[1] => Array
(
[0] => 2
[1] => two
[2] => dos
)
[2] => Array
(
[0] => 3
[1] => three
[2] => tres
)
[3] => Array
(
[0] => 4
[1] => four
[2] => cuatro
)
[4] => Array
(
[0] => 5
[1] => five
[2] => cinco
)
)
例子:foreach函数
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
?> 
例子 1. 函数 trim()
<?php
$text = "\t\tThese are a few words :) ...  ";
echo trim($text);          // "These are a few words :) ..."
echo trim($text, " \t.");  // "These are a few words :)"
// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
$clean = trim($binary, "\x00..\x1F");
?> 
例子 1. preg_quote() 例子
<?php
$keywords = "$40 for a g3/400";
$keywords = preg_quote ($keywords, "/");
echo $keywords; // returns \$40 for a g3\/400
?> 
例子 2. 给某文本中的一个单词加上斜体标记
<?php
// 本例中,preg_quote($word) 用来使星号不在正则表达式中
// 具有特殊含义。
$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/".preg_quote($word)."/",
"<i>".$word."</i>",
$textbody);
?
例子 1. 在文本中搜索“php”
<?php
// 模式定界符后面的 "i" 表示不区分大小写字母的搜索
if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
?> 
例子 2. 搜索单词“web”
<?php
/* 模式中的 \b 表示单词的边界,因此只有独立的 "web" 单词会被匹配,
* 而不会匹配例如 "webbing" 或 "cobweb" 中的一部分 */
if (preg_match ("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
if (preg_match ("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
?> 
例子 3. 从 URL 中取出域名
<?php
// 从 URL 中取得主机名
preg_match("/^(http:\/\/)?([^\/]+)/i",
"www.php/index.html", $matches);
$host = $matches[2];
// 从主机名中取得后面两段
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "domain name is: {$matches[0]}\n";
?> 
例子 1. mt_srand() 范例播下一个更好的随机数发生器种子
<?php
// seed with microseconds
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
mt_srand(make_seed());
$randval = mt_rand();
?> 
例子 1. mt_rand() 范例生成更好的随机数
<?php
echo mt_rand() . "\n";
echo mt_rand() . "\n";
echo mt_rand(5, 15);
?> 
上例的输出类似于:
1604716014
1478613278
6
例子 1. in_array() 例子检查数组中是否存在某个值
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?> 
第二个条件失败,因为 in_array() 是区分大小写的,所以以上程序显示为:
Got Irix 
例子 2. in_array() 严格类型检查例子
<?php
$a = array('1.10', 12.4, 1.13);
if (in_array('12.4', $a, true)) {
echo "'12.4' found with strict check\n";
}
if (in_array(1.13, $a, true)) {
echo "1.13 found with strict check\n";
}
?
上例将输出:
1.13 found with strict check 
例子 3. in_array() 中用数组作为 needle
<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');
if (in_array(array('p', 'h'), $a)) {
echo "'ph' was found\n";
}
if (in_array(array('f', 'i'), $a)) {
echo "'fi' was found\n";
}
if (in_array('o', $a)) {
echo "'o' was found\n";
}
?> 
上例将输出:
'ph' was found
'o' was found
例子 1. chr() 示例返回指定的字符
<?php
$str = "The string ends in escape: ";
$str .= chr(27); /* 在 $str 后边增加换码符 */
/*
通常这样更有用 */
$str = sprintf("The string ends in escape: %c", 27);
?> 

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。