Chinaunix首页 | 论坛 | 博客
  • 博客访问: 318757
  • 博文数量: 84
  • 博客积分: 886
  • 博客等级: 准尉
  • 技术积分: 769
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-30 19:32
文章分类

全部博文(84)

文章存档

2018年(1)

2017年(2)

2014年(1)

2013年(50)

2012年(30)

我的朋友

分类: Python/Ruby

2012-08-17 10:39:14

今天开始接手php项目中的模块进行维护修改升级,很多函数不懂,暂时做个笔记

————————————————————————————————————

array_flip() 返回一个反转后的  array ,例如  trans 中的键名变成了值,而  trans 中的值成了键名。

————————————————————————————————————

get_html_translation_table()函数的作用是:

  返回htmlspecialchars()函数和htmlentities()函数的转换表。正面我们使用get_html_translation_table()进行文本的格式化输出: 

PHP:  

/*
* 功能:格式化文本输出
* 参数 $text 为需格式化的文本内容
*/
function formatcontent($text){
$trans = get_html_translation_table(HTML_SPECIALCHARS);
$trans = array_flip($trans);
$text = strtr($text, $trans);
//$text = str_replace("n", "
", $text);
//$text = str_replace(" ", " ", $text);
return $text;
}
?>

  应用: PHP: 

$str = "

hello
";
$str = htmlspecialchars($str);
echo($str);
$str = formatcontent($str);
echo($str);

————————————————————————————————————

basename -- 返回路径中的文件名部分

string basename ( string path [, string suffix] )

给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名。如果文件名是以 suffix 结束的,那这一部分也会被去掉。 

在 Windows 中,斜线(/)和反斜线(\)都可以用作目录分隔符。在其它环境下是斜线(/)。 

————————————————————————————————————

例子 1. basename() 例子

$path = "/home/httpd/html/index.php";
$file = basename($path);        // $file is set to "index.php"
$file = basename($path,".php"); // $file is set to "index"
?>

———————————————————————————————————— PHP strrpos() 函数

strrpos() 函数查找字符串在另一个字符串中最后一次出现的位置。

如果成功,则返回位置,否则返回 false。 语法strrpos(string,find,start)

参数描述string必需。规定被搜索的字符串。find必需。规定要查找的字符。start可选。规定开始搜索的位置。 提示和注释

注释:该函数对大小写敏感。如需进行大小写不敏感的查找,请使用 。 例子

输出:6

————————————————————————————————————

in_array -- 检查数组中是否存在某个值

bool in_array ( mixed needle, array haystack [, bool strict] )

在 haystack 中搜索 needle,如果找到则返回 TRUE,否则返回 FALSE。 

如果第三个参数 strict 的值为 TRUEin_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。 

注: 如果 needle 是字符串,则比较是区分大小写的。

注: 在 PHP 版本 4.2.0 之前,needle 不允许是一个数组。

例子 1. in_array() 例子

$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() 严格类型检查例子

$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

$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

阅读(635) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~