php字串過濾與替換小結,php字串小結_PHP教程

來源:互聯網
上載者:User

php字串過濾與替換小結,php字串小結


本文執行個體總結了php字串過濾與替換的方法。分享給大家供大家參考。具體實現方法如下:
複製代碼 代碼如下:<?php
class cls_string_filter{
//將\n轉化為
--囧,這有意思嗎?
static public function nl2br($string){
return nl2br($string);
}
//將
轉化為\n
static public function br2nl($string){
$array = array('
','
');
return str_replace($array,"\n",$string);//字串替換
}
//多個空格只保留一個
static public function merge_spaces($string){
return preg_replace("/\s(?=\s)/","\\1",$string);//(?=pattern)舉例:abc(?=kk)能匹配abckk,但不能匹配abcdd
}
//多個
只保留一個
static public function merge_brs($string){
return preg_replace("/(()+)/i","
",$string);//---"/"為什麼也轉義了
}
//過濾字串中的html標籤
static public function strip_tags($string){
return strip_tags($string);
}
//將字串轉換為小寫--/--大寫
static public function strtolower($string){
return strtolower($string);
}
static public function strtoupper($string){
return strtoupper($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);
}
//過濾字串中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.