您的位置:首页 > 编程语言 > PHP开发

php 函数摘录(mainly from wordpress and php manual)--持续更新

2012-04-03 19:08 435 查看

wordpress functions:

1.深度替换(拿替换后的结果再次替换)

/**
* Perform a deep string replace operation to ensure the values in $search are no longer present
*
* Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
* e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
* str_replace would return
*
* @since 2.8.1
* @access private
*
* @param string|array $search
* @param string $subject
* @return string The processed string
*/
function _deep_replace( $search, $subject ) {
$found = true;
$subject = (string) $subject;
while ( $found ) {
$found = false;
foreach ( (array) $search as $val ) {
while ( strpos( $subject, $val ) !== false ) {
$found = true;
$subject = str_replace( $val, '', $subject );
}
}
}

return $subject;
}


2.vars.php

检测浏览器

// Simple browser detection
$is_lynx = $is_gecko = $is_winIE = $is_macIE = $is_opera = $is_NS4 = $is_safari = $is_chrome = $is_iphone = false;

if ( isset($_SERVER['HTTP_USER_AGENT']) ) {
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Lynx') !== false ) {
$is_lynx = true;
} elseif ( stripos($_SERVER['HTTP_USER_AGENT'], 'chrome') !== false ) {
if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'chromeframe' ) !== false ) {
if ( $is_chrome = apply_filters( 'use_google_chrome_frame', is_admin() ) )
header( 'X-UA-Compatible: chrome=1' );
$is_winIE = ! $is_chrome;
} else {
$is_chrome = true;
}
} elseif ( stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false ) {
$is_safari = true;
} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false ) {
$is_gecko = true;
} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Win') !== false ) {
$is_winIE = true;
} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ) {
$is_macIE = true;
} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false ) {
$is_opera = true;
} elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Nav') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.') !== false ) {
$is_NS4 = true;
}
}

if ( $is_safari && stripos($_SERVER['HTTP_USER_AGENT'], 'mobile') !== false )
$is_iphone = true;

$is_IE = ( $is_macIE || $is_winIE );


3.检测服务器

// Server detection

/**
* Whether the server software is Apache or something else
* @global bool $is_apache
*/
$is_apache = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false);

/**
* Whether the server software is IIS or something else
* @global bool $is_IIS
*/
$is_IIS = !$is_apache && (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer') !== false);

/**
* Whether the server software is IIS 7.X
* @global bool $is_iis7
*/
$is_iis7 = $is_IIS && (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/7.') !== false);


php manual functions:

1.深度去除反斜线(json string)

<?php
function stripslashes_deep(&$value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);

return $value;
}
?>


2.转义字符串,用于sql

<?php
function conditional_escape ($str)
{
/* Automatic escaping is highly deprecated, but many sites do it
anyway to protect themselves from stupid customers. */
if (get_magic_quotes_gpc())
{
/* Apache automatically escaped the string already. */
return $str;
}
/* Replace the following line with whatever function you prefer
to call to escape a string. */
return mysqli_real_escape_string ($link, $str);
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: