trim in an explode

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

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

trim in an explode

Post by hame22 »

Hi I am using an explode statement to get individual words from a selection of words which i then search within my site for.

Code: Select all

$q_array=explode(" ", $q);
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 »

this will probably help you out..unless i'm not following your question or being stupidly silly this early in the morning :-D
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 »

-
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 »

Code: Select all

foreach ($array as $k => $value) {
     if (trim($value)=='') {
          // delete element with the index $k
     }
}
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Code: Select all

$q_array = preg_split('/\s+/', $q);
Post Reply