note 107464 added to function.trim
| From: | bartosz dot zawadzki at gmail dot com | Date: | Fri, 10 Feb 2012 18:11:00 +0000 |
| Subject: | note 107464 added to function.trim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186058@lists.php.net to get a copy of this message | ||
simple function that trims and removes repeating chairs from the middle of a string. i'm sure
that it's not optimized, but it works for me
<?php
function mtrim($str,$chair=" ")
// function that trims and removes repeating chairs from the middle of a string //
{
for ($i = 0; $i < strlen($chair); $i++)
{
$count = 1;
$str = trim($str);
while($count) {
$str = str_replace($chair{$i}.$chair{$i}, $chair{$i}, $str, $count);
}
}
return trim($str,$chair);
}
$string = "Anno\n\n\ndomini 2012";
echo mtrim($string," \n"); //double quote "
/*
output:
Anno
domini 2012
*/
echo mtrim($string,' \n'); //single quote '
/*
output:
Ano
domini 2012
*/
?>
I know it can be done with preg_replace, but not everybody wish to work around escape sequences <
http://www.php.net/manual/en/regexp.reference.escape.php
> to remove doubled letter :)
<?php
// remove any whitespaces
$str = trim(preg_replace("'\s+'", ' ', $str));
?>
----
Server IP: 69.147.83.197
Probable Submitter: 62.21.37.222
----
Manual Page -- http://www.php.net/manual/en/function.trim.php
Edit -- https://master.php.net/note/edit/107464
Del: integrated -- https://master.php.net/note/delete/107464/integrated
Del: useless -- https://master.php.net/note/delete/107464/useless
Del: bad code -- https://master.php.net/note/delete/107464/bad+code
Del: spam -- https://master.php.net/note/delete/107464/spam
Del: non-english -- https://master.php.net/note/delete/107464/non-english
Del: in docs -- https://master.php.net/note/delete/107464/in+docs
Del: other reasons-- https://master.php.net/note/delete/107464
Reject -- https://master.php.net/note/reject/107464
Search -- https://master.php.net/manage/user-notes.php