PHP清除字符串中所有無用標(biāo)簽的方法
更新時間:2014年12月01日 14:24:50 投稿:shichen2014
這篇文章主要介紹了PHP清除字符串中所有無用標(biāo)簽的方法,涉及針對字符串中空格、html標(biāo)簽等的清除,是非常實用的技巧,需要的朋友可以參考下
本文實例講述了PHP清除字符串中所有無用標(biāo)簽的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
很多時候需要輸出一些 “純” 字符串,也就是去除任何雜質(zhì),例如 Html 標(biāo)簽、空格之類的文本,輸出的摘要就是如此,下面的這個函數(shù)可以幫你實現(xiàn)著一點.
PHP實例代碼如下:
復(fù)制代碼 代碼如下:
function Bing_string_cleanr( $string ){
$string = trim( $string );
$string = strip_tags( $string );
$string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8' );
$string = str_replace( "n", "", $string );
$string = trim( $string );
return $string;
}
$string = trim( $string );
$string = strip_tags( $string );
$string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8' );
$string = str_replace( "n", "", $string );
$string = trim( $string );
return $string;
}
使用方法如下:
復(fù)制代碼 代碼如下:
echo Bing_string_cleanr( '內(nèi) 容 <br> <html> asdfeiuonsdfje' );
php刪除空白,代碼如下:
復(fù)制代碼 代碼如下:
<?php
$str = " This line containstliberal rn use of whitespace.nn";
// First remove the leading/trailing whitespace
//去掉開始和結(jié)束的空白
$str = trim($str);
// Now remove any doubled-up whitespace
//去掉跟隨別的擠在一塊的空白
$str = preg_replace('/s(?=s)/', '', $str);
// Finally, replace any non-space whitespace, with a space
//最后,去掉非space 的空白,用一個空格代替
$str = preg_replace('/[nrt]/', ' ', $str);
// Echo out: 'This line contains liberal use of whitespace.'
echo "<pre>{$str}</pre>";
?>
$str = " This line containstliberal rn use of whitespace.nn";
// First remove the leading/trailing whitespace
//去掉開始和結(jié)束的空白
$str = trim($str);
// Now remove any doubled-up whitespace
//去掉跟隨別的擠在一塊的空白
$str = preg_replace('/s(?=s)/', '', $str);
// Finally, replace any non-space whitespace, with a space
//最后,去掉非space 的空白,用一個空格代替
$str = preg_replace('/[nrt]/', ' ', $str);
// Echo out: 'This line contains liberal use of whitespace.'
echo "<pre>{$str}</pre>";
?>
希望本文所述對大家的PHP程序設(shè)計有所幫助。
相關(guān)文章
mysql 查詢指定日期時間內(nèi)sql語句實現(xiàn)原理與代碼
查詢指定日期時間內(nèi)sql語句實現(xiàn)原理:如果是月份就是當(dāng)前的月減去你要統(tǒng)計的時間如我要查詢數(shù)據(jù)庫中從今天起往前三個月的所有記錄2012-12-12
Windows下的PHP安裝文件線程安全和非線程安全的區(qū)別
Windows版的PHP從版本5.2.1開始有Thread Safe。這兩者不同在于何處?到底應(yīng)該用哪種?這里做一個簡單的介紹2014-04-04

