WPJAM Basic 内置了一些不错的常用函数,在进行 WordPress 主题和插件二次开发的时候,可以参考一下,下面就介绍一下这些函数。
去掉非 utf8mb4 字符
WordPress 字符数据字段格式已经升级为:utf8mb4,就是说支持 emoji 等4个字节的字符,但是在开发的时候发现用户还是会输入一些 utf8mb4 还不支持的字符,可以使用该函数过滤掉。
// 去掉非 utf8mb4 字符
function theme_strip_invalid_text($str){
$regex = '/
(
(?: [\x00-\x7F] # single-byte sequences 0xxxxxxx
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
| [\xE1-\xEC][\x80-\xBF]{2}
| \xED[\x80-\x9F][\x80-\xBF]
| [\xEE-\xEF][\x80-\xBF]{2}
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
| [\xF1-\xF3][\x80-\xBF]{3}
| \xF4[\x80-\x8F][\x80-\xBF]{2}
){1,50} # ...one or more times
)
| . # anything else
/x';
return preg_replace($regex, '$1', $str);
}
去掉控制字符
如果字符中有控制字符,json_decode 和 simplexml_load_string 函数就会失败,我们首先要把这些控制字符去掉:
// 去掉控制字符
function theme_strip_control_characters($str){
return preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F]/u', '', $str); // 移除除了 line feeds 和 carriage returns 所有控制字符
// return preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x80-\x9F]/u', '', $str); // 移除除了 line feeds 和 carriage returns 所有控制字符
}
获取纯文本
去掉 html 标签,去掉换行,去掉多余的空格,去掉双引号和单引号:
//获取纯文本
function theme_get_plain_text($text){
$text = wp_strip_all_tags($text);
$text = str_replace('"', '', $text);
$text = str_replace('\'', '', $text);
// replace newlines on mac / windows?
$text = str_replace("\r\n", ' ', $text);
// maybe linux uses this alone
$text = str_replace("\n", ' ', $text);
$text = str_replace(" ", ' ', $text);
return $text;
}
获取第一段
//获取第一段
function get_first_p($text){
if($text){
$text = explode("\n",strip_tags($text));
$text = trim($text['0']);
}
return $text;
}
检查是否含有非法字符
判断字符中是否含有在后台设置 > 评论设置中的审核黑名单中的字符,有返回 true。
function theme_blacklist_check($str){
$moderation_keys = trim(get_option('moderation_keys'));
$blacklist_keys = trim(get_option('blacklist_keys'));
$keys = $moderation_keys ."\n".$blacklist_keys;
$words = explode("\n", $keys );
foreach ( (array) $words as $word) {
$word = trim($word);
// Skip empty lines
if ( empty($word) )
continue;
// Do some escaping magic so that '#' chars in the
// spam words don't break things:
$word = preg_quote($word, '#');
$pattern = "#$word#i";
if ( preg_match($pattern, $str) ) return true;
}
return false;
}
获取当前页面的链接
// 获取当前页面 url
function theme_get_current_page_url(){
$ssl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? true:false;
$sp = strtolower($_SERVER['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $_SERVER['SERVER_PORT'];
$port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
$host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
return $protocol . '://' . $host . $port . $_SERVER['REQUEST_URI'];
}
判断是否是机器人
/*判断是否是机器人*/
function is_bot(){
$useragent = trim($_SERVER['HTTP_USER_AGENT']);
if(stristr($useragent, 'bot') !== false || stristr($useragent, 'spider') !== false){
return true;
}
return false;
}
获取页面来源
// 获取页面来源
function theme_get_referer(){
$referer = wp_get_original_referer();
$referer = ($referer)?$referer:wp_get_referer();
$removable_query_args = array_merge(
theme_get_removable_query_args(),
array('_wp_http_referer','id','action', 'action2', '_wpnonce')
);
return remove_query_arg($removable_query_args, $referer);
}
本文首发于:WPJAM Basic php 常用函数-垃圾站
Comments:0