php basics
PHP clear string blank method
Although PHP's trim function can clear the whitespace before and after the string , in some cases, not only the whitespace before and after the string needs to be cleared, but also all the whitespace between the strings must be cleared . Use the PHP regular expression technique to deal with, the main program operation is actually only two parts, please see the following example.
Example, first clear the blank characters before and after the string and then remove the blanks between the strings
<?php
$TestStr = "This string have many whitespace.";
echo'<pre>'.$TestStr.'<br></pre>';
$TestStr = trim($TestStr);
echo'<pre>' .$TestStr.'<br></pre>';
$TestStr = preg_replace('/\s(?=)/','', $TestStr);
echo'<pre>'.$TestStr.'</ pre>';
?>
Output result$TestStr = "This string have many whitespace.";
echo'<pre>'.$TestStr.'<br></pre>';
$TestStr = trim($TestStr);
echo'<pre>' .$TestStr.'<br></pre>';
$TestStr = preg_replace('/\s(?=)/','', $TestStr);
echo'<pre>'.$TestStr.'</ pre>';
?>
This string have many whitespace.
This string have many whitespace.
Thisstringhavemanywhitespace.
Post a Comment
0 Comments