Home >php教程 >PHP源码 >参数过滤类

参数过滤类

PHP中文网
PHP中文网Original
2016-05-25 17:09:301035browse

PHP代码

<?php
/**
* 参数过滤类
*
*/
class cls_string_filter
{
/**
* 将\n转化为<br/>
*
* @param string $string 待转换的字符串
* @return string
*/
static public function nl2br($string)
{
return nl2br($string);
}
/**
* 将<br/>转化为\n
*
* @param string $string 待转换的字符串
* @return string
*/
static public function br2nl($string)
{
$array = array(&#39;<br>&#39;, &#39;<br/>&#39;);
return str_replace($array, "\n", $string);
}
/**
* 多个连续空格只保留一个
*
* @param string $string 待转换的字符串
* @return unknown
*/
static public function merge_spaces($string)
{
return preg_replace("/\s(?=\s)/","\\1", $string);
}
/**
* 多个连续<br/>只保留一个
*
* @param string $string 待转换的字符串
* @return string
*/
static public function merge_brs($string)
{
return preg_replace("/((<br\/?>)+)/i", "<br/>", $string);
}
/**
* 过滤字符串中的html标签
*
* @param string $string 待转换的字符串
* @return string
*/
static public function strip_tags($string)
{
return strip_tags($string);
}
/**
* 将字符串转换为全小写
*
* @param string $string 待转换的字符串
* @return string
*/
static public function strtolower($string)
{
return strtolower($string);
}
/**
* 将字符串转换为全大写
*
* @param string $string 待转换的字符串
* @return string
*/
static public function strtoupper($string)
{
return strtoupper($string);
}
/**
* 过滤字符串中开头和结尾的特定字符
*
* @param string $string    待转换的字符串
* @param string $char_list 要转换的特定字符列表
* @return string
*/
static public function trim($string, $char_list=&#39;\\\\s&#39;)
{
$chars = preg_replace(
array( &#39;/[\^\-\]\\\]/S&#39;, &#39;/\\\{4}/S&#39;, &#39;/\//&#39;),
array( &#39;\\\\\\0&#39;, &#39;\\&#39;, &#39;\/&#39; ),
$char_list
);
$pattern = &#39;^[&#39; . $chars . &#39;]*|[&#39; . $chars . &#39;]*
;
return preg_replace("/$pattern/sSD", &#39;&#39;, $string);
}
/**
* 过滤字符串中的回车换行
*
* @param string $string 待转换的字符串
* @return string
*/
static public function strip_new_lines($string)
{
return str_replace(array("\n", "\r"), &#39;&#39;, $string);
}
/**
* 过滤字符串中<script>脚本
*
* @param string $string 待过滤的字符串
* @return string
*/
static public function strip_script($string)
{
$reg = "/<script[^>]*?>.*?<\/script>/is";
return preg_replace($reg, &#39;&#39;, $string);
}
/**
* 过滤字符串中<style>脚本
*
* @param string $string 待过滤的字符串
* @return string
*/
static public function strip_style($string)
{
$reg = "/<style[^>]*?>.*?<\/style>/is";
return preg_replace($reg, &#39;&#39;, $string);
}
/**
* 过滤字符串中<link>脚本
*
* @param string $string 待过滤的字符串
* @return string
*/
static public function strip_link($string)
{
$reg = "/<link[^>]*?>.*?<\/link>/is";
return preg_replace($reg, &#39;&#39;, $string);
}
/**
* 过滤字符串中<iframe>
*
* @param string $string 待过滤的字符串
* @return string
*/
static public function strip_iframe($string)
{
$reg = "/<iframe[^>]*?>.*?<\/iframe>/is";
return preg_replace($reg, &#39;&#39;, $string);
}
/**
* 过滤字符串中html危险代码
*
* @param string $string 待过滤的字符串
* @return string
*/
static public function strip_html_tags($string)
{
$reg = "/<(\/?)(script|iframe|style|html|body|title|link|meta|\?|\%)([^>]*?>)/is";
return preg_replace($reg, &#39;&#39;, $string);
}
}
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn