note 107464 deleted from function.trim by tularis

From: Date: Sun, 12 Feb 2012 11:28:29 +0000
Subject: note 107464 deleted from function.trim by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-186080@lists.php.net to get a copy of this message
Note Submitter: bartosz dot zawadzki at gmail dot com ---- 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)); ?>

« previous php.notes (#186080) next »