note 87164 modified in function.trim by danbrown
| From: | danbrown@php.net | Date: | Thu, 01 Jan 2009 17:18:12 +0000 |
| Subject: | note 87164 modified in function.trim by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148742@lists.php.net to get a copy of this message | ||
A simple function to clear extra white spaces along a string.
<?php
function TrimStr($str)
{
$str = trim($str);
for($i=0;$i < strlen($str);$i++)
{
if(substr($str, $i, 1) != " ")
{
$ret_str .= trim(substr($str, $i, 1));
}
else
{
while(substr($str,$i,1) == " ")
{
$i++;
}
$ret_str.= " ";
$i--; // ***
}
}
return $ret_str;
}
?>
[EDIT BY danbrown AT php DOT net: Contains a fix provided by (info AT deep-soft DOT com) to address
the issue where "it deletes the first char after spaces (because of while)."]
--was--
A simple function to clear extra white spaces along a string.
<?php
function TrimStr($str)
{
$str = trim($str);
for($i=0;$i < strlen($str);$i++)
{
if(substr($str, $i, 1) != " ")
{
$ret_str .= trim(substr($str, $i, 1));
}
else
{
while(substr($str,$i,1) == " ")
{
$i++;
}
$ret_str.= " ";
}
}
return $ret_str;
}
?>
http://php.net/manual/en/function.trim.php