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

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

 1 /**
2 * Perform a deep string replace operation to ensure the values in $search are no longer present
3 *
4 * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
5 * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
6 * str_replace would return
7 *
8 * @since 2.8.1
9 * @access private
10 *
11 * @param string|array $search
12 * @param string $subject
13 * @return string The processed string
14 */
15 function _deep_replace( $search, $subject ) {
16 $found = true;
17 $subject = (string) $subject;
18 while ( $found ) {
19 $found = false;
20 foreach ( (array) $search as $val ) {
21 while ( strpos( $subject, $val ) !== false ) {
22 $found = true;
23 $subject = str_replace( $val, '', $subject );
24 }
25 }
26 }
27
28 return $subject;
29 }

2.vars.php

检测浏览器

 1 // Simple browser detection
 2 $is_lynx = $is_gecko = $is_winIE = $is_macIE = $is_opera = $is_NS4 = $is_safari = $is_chrome = $is_iphone = false;
 3 
 4 if ( isset($_SERVER['HTTP_USER_AGENT']) ) {
 5     if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Lynx') !== false ) {
 6         $is_lynx = true;
 7     } elseif ( stripos($_SERVER['HTTP_USER_AGENT'], 'chrome') !== false ) {
 8         if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'chromeframe' ) !== false ) {
 9             if ( $is_chrome = apply_filters( 'use_google_chrome_frame', is_admin() ) )
10                 header( 'X-UA-Compatible: chrome=1' );
11             $is_winIE = ! $is_chrome;
12         } else {
13             $is_chrome = true;
14         }
15     } elseif ( stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false ) {
16         $is_safari = true;
17     } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false ) {
18         $is_gecko = true;
19     } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Win') !== false ) {
20         $is_winIE = true;
21     } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false ) {
22         $is_macIE = true;
23     } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false ) {
24         $is_opera = true;
25     } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Nav') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.') !== false ) {
26         $is_NS4 = true;
27     }
28 }
29 
30 if ( $is_safari && stripos($_SERVER['HTTP_USER_AGENT'], 'mobile') !== false )
31     $is_iphone = true;
32 
33 $is_IE = ( $is_macIE || $is_winIE );

3.检测服务器

 1 // Server detection
 2 
 3 /**
 4  * Whether the server software is Apache or something else
 5  * @global bool $is_apache
 6  */
 7 $is_apache = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false);
 8 
 9 /**
10  * Whether the server software is IIS or something else
11  * @global bool $is_IIS
12  */
13 $is_IIS = !$is_apache && (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false || strpos($_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer') !== false);
14 
15 /**
16  * Whether the server software is IIS 7.X
17  * @global bool $is_iis7
18  */
19 $is_iis7 = $is_IIS && (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/7.') !== false);

php manual functions:

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

 1 <?php 
2 function stripslashes_deep(&$value)
3 {
4 $value = is_array($value) ?
5 array_map('stripslashes_deep', $value) :
6 stripslashes($value);
7
8 return $value;
9 }
10 ?>

2.转义字符串,用于sql

 1 <?php 
2 function conditional_escape ($str)
3 {
4 /* Automatic escaping is highly deprecated, but many sites do it
5 anyway to protect themselves from stupid customers. */
6 if (get_magic_quotes_gpc())
7 {
8 /* Apache automatically escaped the string already. */
9 return $str;
10 }
11 /* Replace the following line with whatever function you prefer
12 to call to escape a string. */
13 return mysqli_real_escape_string ($link, $str);
14 }
15 ?>