note 29402 added to function.strpos

From: Date: Wed, 12 Feb 2003 17:02:22 +0000
Subject: note 29402 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-43828@lists.php.net to get a copy of this message
There are a couple problems with the function(s) and code above for stripping spaces. The main problem is multiple spaces. For example, if someone entered "this is my answer! !!!". There would still be multiple spaces in the text after using the previously mentioned methods. The solution is to loop until there are no more multiple spaces. Here is my solution ... function StripSpaces ( $inputString ) { $inputString = trim($inputString); $pos = strpos($inputString, " "); while (is_integer($pos) && $pos > 0) { $inputString = str_replace(" ", " ", $inputString); $pos = strpos($inputString, " "); } return $inputString; } Sure, it could have been shorter but it is more readable in this form. Good Luck!!! -- http://www.php.net/manual/en/function.strpos.php http://master.php.net/manage/user-notes.php?action=edit+29402 http://master.php.net/manage/user-notes.php?action=delete+29402 http://master.php.net/manage/user-notes.php?action=reject+29402

« previous php.notes (#43828) next »