This article summarizes the method of filtering and replacing PHP strings. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
<?php
Class cls_string_filter{
Turn \ n into <br/>--, is that funny?
static public Function nl2br ($string) {
Return nl2br ($string);
}
Convert <br/> to \ n
static public Function br2nl ($string) {
$array = Array (' <br> ', ' <br/> ');
Return Str_replace ($array, "\ n", $string);//String substitution
}
Multiple spaces retain only one
static public Function merge_spaces ($string) {
Return Preg_replace ("/\s =\s)/", "\\1", $string);//(? =pattern) For example: ABC (? =KK) can match Abckk, but not match ABCDD
}
Multiple <br/> keep only one
static public Function Merge_brs ($string) {
Return Preg_replace ("/(<br\/?>)/I", "<br>", $string);/---"/" why also escaped
}
Filter HTML tags in a string
static public Function Strip_tags ($string) {
Return Strip_tags ($string);
}
Converts a string to lowercase--/--uppercase
static public Function Strtolower ($string) {
Return Strtolower ($string);
}
static public Function Strtoupper ($string) {
Return Strtoupper ($string);
}
Filter specific characters at the beginning and end of a string
static public Function trim ($string, $char _list= ' \\\\s ') {
$find = Array ('/[\^\-\]\\\]/s ', '/\\\{4}/s ', '/\//');
$replace = Array (' \\\\\\0 ', ' \ \ \ ', ' \/');
$char = Preg_replace ($fine, $replace, $char _list);
$pattern = ' ^['. $chars. ']*| ['. $chars. '] ';
Return Preg_replace ("/$pattern/SSD", ", $string);
}
Filtering <style> Scripting in strings
static public Function Stric_style ($string) {
$reg = "/<style[^>]*?>.*?<\/style>/is";
Return Preg_replace ($reg, ', $string);
}
HTML Risk code in filter string
static public Function Strip_html_tags ($string) {
$reg = "/(\/?) /(script|iframe|style|html|body|title|meta|\?| \%) ([^>]*?>)/is ";
Return Preg_replace ($reg, ', $string);
}
}
?>
I hope this article will help you with your PHP program design.