PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
hame22
Forum Contributor
Posts: 214 Joined: Wed May 11, 2005 5:50 am
Post
by hame22 » Tue Dec 13, 2005 5:43 am
Hi I am using an explode statement to get individual words from a selection of words which i then search within my site for.
works fine unless a user types in words that have more than a double space between example
"first [space][space][space] second"
what i need tp do is trim the white space between these words, any idea how i do this?
thanks in advance
Charles256
DevNet Resident
Posts: 1375 Joined: Fri Sep 16, 2005 9:06 pm
Post
by Charles256 » Tue Dec 13, 2005 7:42 am
this will probably help you out..unless i'm not following your question or being stupidly silly this early in the morning
http://us3.php.net/trim
djot
Forum Contributor
Posts: 313 Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:
Post
by djot » Wed Dec 14, 2005 2:03 pm
-
Why not replace multi-spaces before exploding?
Code: Select all
//for 2 spaces
str_replace(" ", " ", $q);
//for 3 spaces
str_replace(" ", " ", $q);
str_replace(" ", " ", $q);
// loop for n spaces
$q_array=explode(" ", $q);
djot
-
josh
DevNet Master
Posts: 4872 Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida
Post
by josh » Wed Dec 14, 2005 2:19 pm
Code: Select all
foreach ($array as $k => $value) {
if (trim($value)=='') {
// delete element with the index $k
}
}
shiznatix
DevNet Master
Posts: 2745 Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:
Post
by shiznatix » Wed Dec 14, 2005 2:29 pm
Code: Select all
$clean_string = preg_replace('/\s+/', '', $string); //Just make the second parameter a single space to turn excess whitespace into a single space
taken from a very old post i had, well it was not that old - less than 1 year actually but it sure feels like a long time ago.