boen_robot Posted March 9, 2008 Share Posted March 9, 2008 You all know that HTML truncates whitespace, that is, it turns some text into just some text I was wondering, is there an easy way to duplicate this functionality in PHP? That is, a function that would take a certain string, and truncate all spaces inside it, returning the truncated string.I suppose it could be done with regular expression replacements, but I'd like to avoid that if possible (truncation is not as important for what I need this for, so using this would be overly complex and maybe not very efficient too). Link to comment Share on other sites More sharing options...
Synook Posted March 10, 2008 Share Posted March 10, 2008 What's wrong with regular expressions? function truncate_spaces($string) { return preg_replace("/\s{2,}/", " ", $string);} Can't think of any other way... Link to comment Share on other sites More sharing options...
justsomeguy Posted March 10, 2008 Share Posted March 10, 2008 The only other way would be to go through the string character-by-character and rebuild it. The regex would be more efficient then that. Link to comment Share on other sites More sharing options...
boen_robot Posted March 10, 2008 Author Share Posted March 10, 2008 I was really hoping PHP would include a function for that which I have overlooked. Oh well... regex replacement it is. Link to comment Share on other sites More sharing options...
jhecht Posted March 11, 2008 Share Posted March 11, 2008 do you mean like if you have a this string (the words [space] is used to indicate a single space) bob[space][space]is[space][space][space][space][space]cool[space]and[space]fun to turn it into this bob[space]is[space]cool[space]and[space]fun ?Because if so i believe the trim() function does this... (IF I'm not mistaken, never really use trim for anything)also, you could just use(keep in mind i've never tried this, so if it doesn't work, don't yell at me). $str = " multiple spaces";while(strpos($str," ")){ $str = str_replace(" "," ",$str);} And as said, whats wrong with regular expressions? Link to comment Share on other sites More sharing options...
zppblood Posted March 11, 2008 Share Posted March 11, 2008 To JhechtFrom php.net on the trim():"Strip whitespace (or other characters) from the beginning and end of a string"The str_replace function would not work as to what boen_robot with the following examples:The[space][space][space]dog[space]crossed[space]the[space][space]street.would turn toThe[space][space]dog[space]crossed[space]the[space]street.Look at the spaces after the word "The".The[space][space][space][space]dog[space]crossed[space]the[space]street.Again after the word "The" you would have two spaces instead of one.EDIT: Didn't think see the while loop, and do see how you are doing it. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2008 Share Posted March 11, 2008 This is the alternative to the regular expression: $words = explode(' ', $str);for ($i = count($words) - 1; $i >= 0; $i--){ $words[$i] = trim($words[$i]); if ($words[$i] == '') unset($words[$i]);}$str = implode(' ', $words); Obviously the regex is going to be quicker and more efficient. Link to comment Share on other sites More sharing options...
boen_robot Posted March 11, 2008 Author Share Posted March 11, 2008 I was already persuaded by the time I read "The only other way " ("only" being the word that persuaded me). But thanks for the alternative(s?) anyway. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.