PHP string filter and replacement summary, PHP string Summary _php Tutorial

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.