note 85333 added to function.trim

From: Date: Tue, 26 Aug 2008 13:21:41 +0000
Subject: note 85333 added to function.trim
Groups: php.notes 
Request: Send a blank email to php-notes+get-143837@lists.php.net to get a copy of this message
The following function will assist you in case you wish to trim a string but keep the parts trimmed. I'm using it to strip phrases for translation on my site. Example: cleanPhrase(' (this () is it)'); Will return: Array ( [left] => ( [right] => ) [clean] => this () is it ) /** * Trims some of the characters from the phrase * * @param string $phrase - to be "cleaned" * @param string $dirtyChars - the characters that need be removed * @return string */ public static function cleanPhrase($phrase, $dirtyChars = " \t\n()[]:.\"'") { $len = strlen($phrase); for($i=0; $i < $len; $i++) { $pos = strpos($dirtyChars, substr($phrase, $i, 1)); if ($pos === false) break; } for($j=$len-1; $j >= 0; $j--) { $pos = strpos($dirtyChars, substr($phrase, $j, 1)); if ($pos === false) break; } $res = array(); $res['left'] = substr($phrase, 0, $i); $res['right'] = substr($phrase, $j+1); $res['clean'] = substr($phrase, $i, $j-$i+1); return $res; } ---- Server IP: 82.80.248.162 Probable Submitter: 89.1.10.210 ---- Manual Page -- http://www.php.net/manual/en/function.trim.php Edit -- https://master.php.net/note/edit/85333 Del: integrated -- https://master.php.net/note/delete/85333/integrated Del: useless -- https://master.php.net/note/delete/85333/useless Del: bad code -- https://master.php.net/note/delete/85333/bad+code Del: spam -- https://master.php.net/note/delete/85333/spam Del: non-english -- https://master.php.net/note/delete/85333/non-english Del: in docs -- https://master.php.net/note/delete/85333/in+docs Del: other reasons-- https://master.php.net/note/delete/85333 Reject -- https://master.php.net/note/reject/85333 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143837) next »