PHP string filter and replacement summary, PHP string summary
This paper summarizes the methods of filtering and replacing PHP strings. Share to everyone for your reference. The implementation method is as follows:
Copy CodeThe code is as follows: <?php
Class cls_string_filter{
Turn \ n to
--embarrassing, is that interesting?
static public Function nl2br ($string) {
Return nl2br ($string);
}
Will
Convert to \ n
static public Function br2nl ($string) {
$array = Array ('
','
');
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) Example: ABC (? =KK) can match Abckk, but cannot match ABCDD
}
Multiple
Keep Only one
static public Function Merge_brs ($string) {
Return Preg_replace ("/(( ) +)/I ","
", $string);//---"/"Why is it 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 for 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);
}
In the filter string