Just to clear things up:
Is there php function to remove the space inside the string? for example:
$abcd="this is a test"
I want to get the string:
$abcd="thisisatest"
How to do that? thanks.
str_replace() will remove *all* occurrence of whitespace.
$str = str_replace(' ', '', $str);
So it solves the problem in the original post.
The post:Try this:
NOTE: this actually leaves one space between words. If you move the
$newstr = $newstr . substr($s, $i, 1);
after the while loop it will strip all spaces as you wanted.
function StripExtraSpace($s)
{
for($i = 0; $i < strlen($s); $i++)
{
$newstr = $newstr . substr($s, $i, 1);
if(substr($s, $i, 1) == ' ')
while(substr($s, $i + 1, 1) == ' ')
$i++;
}
return $newstr;
}
Offers a solution that removes excess whitespace.
So if you want to turn:
$str = "this is a string with excess whitespace";
into:
"this is a string with excess whitespace";
You could use the StripExtraSpace() function provided above.For starters, functions in loops should be avoided whenever possible. e.g. for($i = 0; $i < strlen($s); $i++) also, you're doing a lot of extra work here.
I would suggest something like this:
<?php
$sTestString = 'This is a stringwith lots of odd spaces and tabs
and some newlines too
lets see if this works.';
$sPattern = '/\s*/m';
$sReplace = '';
echo $sTestString . '';
echo preg_replace( $sPattern, $sReplace, $sTestString );
Regular Expression removes ALL whitespace ( spaces, tabs, newlines) 0 or more times and also traverses multiple lines.
Put that in a file and run it in your browser, view the source and you'll see exactly what it has done.
No need to reinvent the wheel.
Regex is really slow, I mean really really slow. So even a loop is likely to be faster.
I benchmarked the regex function and StripExtraSpace(). The regex function is twice as fast. However, an optimized version of StripExtraSpace() is faster then the regular expression.
function loopStripExtraWhitespaceOptimized($str) {
$len = strlen($str);
for($i = 0; $i < $len; $i++) {
$newstr .= $str[$i];
while($str[$i] == ' ') {
$i++;
}
}
return $newstr;
}
Note the if/else is removed as this is implied in the while() loop. Also using the string's built in array index is a lot faster then substr().
The computational/ time complexity of a regular expression is quite high. So an O(n), where n is the length of the string, such as the above can be just as fast, or faster.
Using a native PHP string function can speed things up:
function stripExtraWhitespace($str) {
while($str != ($_str = str_replace(' ', ' ', $str))) {
$str = $_str;
}
return $str;
}
This is about 30x faster the above two. This has a worst case O(log 2 n), but realistically it would be just 2-3 operations of str_replace().
Here is the benchmarks:
<?php
function strStripWhitespace($str) {
return str_replace(' ', '', $str);
}
function regexStripExtraWhitespace($str) {
return preg_replace('/\s*/m', ' ', $str);
}
function loopStripExtraWhitespace($str) {
$len = strlen($str);
for($i = 0; $i < $len; $i++) {
$newstr .= substr($str, $i, 1);
while(substr($str, $i + 1, 1) == ' ') {
$i++;
}
}
return $newstr;
}
function loopStripExtraWhitespaceOptimized($str) {
$len = strlen($str);
for($i = 0; $i < $len; $i++) {
$newstr .= $str[$i];
while($str[$i] == ' ') {
$i++;
}
}
return $newstr;
}
function loopStripExtraWhitespace2($str) {
while($str != ($_str = str_replace(' ', ' ', $str))) {
$str = $_str;
}
return $str;
}
function benchMark($function, $arg = null, $loops = 1000) {
$start = microtime(1);
for($i = 0; $i < $loops; $i++) {
$function($arg);
}
return microtime(1)-$start;
}
ini_set('max_execution_time', 0);
$str = "\t hello this is a string.\r\n";
for($i = 0; $i < 10; $i++) {
$str .= $str;
}
echo benchMark('strStripWhitespace', $str, 1000);
echo '';
echo benchMark('regexStripExtraWhitespace', $str, 1000);
echo '';
echo benchMark('loopStripExtraWhitespace', $str, 1000);
echo '';
echo benchMark('loopStripExtraWhitespaceOptimized', $str, 1000);
echo '';
echo benchMark('loopStripExtraWhitespace2', $str, 1000);
echo '';
?>