A function with PHP itself
The code is as follows |
Copy Code |
Str_replace ("", "", $str); To replace |
The code is as follows |
Copy Code |
<?php $str = "# #使用函数trim去掉字符串两端特定字符"; $str 1 = Trim ($str, "#"); Pass in the second argument for the function trim, trim will remove the string $str the # character at both ends echo $str. " <br> "; echo $str 1; ?> |
Instance
The code is as follows |
Copy Code |
<?php $STR = "Use function trim to remove whitespace characters from both ends of the string"; $str 1 = Trim ($STR); echo "Before processing has". strlen ($STR). Characters "; echo "<br/>"; echo "<br/>"; echo "processed with the Trim function". strlen ($str 1). Characters "; ?> |
Look at a higher level.
PHP program Deletes "space" in "string element" in "array"
The code is as follows |
Copy Code |
? $arr =array (); $arr []= "ad DFD DFD"; $arr []= "SAF SDF DSF"; $arr []= "SDF DSFGFD DD"; $arr []= "DFD dfferw"; while (list ($name, $value) =each ($arr)) { Echo $value; $arr 2[]=trim ($value);//Go to space } Print_r ($arr 2);//This should be the array you want. ?> |
Try to remove a space with a regular expression
The code is as follows |
Copy Code |
$string = Preg_replace ("/s+ ([$])/", "\1", $string);
|
When ' $ ' is inside [], www.111cn.net it does not represent the ' end of string
---------------------------------------------------------------
The code is as follows |
Copy Code |
$string = Preg_replace ("/s+" ([]|$)/"," \1 ", $string); |
---------------------------------------------------------------
The code is as follows |
Copy Code |
$string = Preg_replace ("/+ ([]|$)/", "\1", $string); |
---------------------------------------------------------------
The code is as follows |
Copy Code |
$string = Preg_replace ('/([]) [s]+/', ' \1 ', $string); |