Page 1 of 1

trim in an explode

Posted: Tue Dec 13, 2005 5:43 am
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

Posted: Tue Dec 13, 2005 7:42 am
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

Posted: Wed Dec 14, 2005 2:03 pm
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
-

Posted: Wed Dec 14, 2005 2:19 pm
by josh

Code: Select all

foreach ($array as $k => $value) {
     if (trim($value)=='') {
          // delete element with the index $k
     }
}

Posted: Wed Dec 14, 2005 2:29 pm
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.

Posted: Wed Dec 14, 2005 3:01 pm
by redmonkey

Code: Select all

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