PHP string Filter and replace summary _php tips

Source: Internet
Author: User

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.

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.