niche Posted May 11, 2010 Share Posted May 11, 2010 I'd like to turn this into a function to reduce multiple spaces to one space between characters: $var = trim($var);//echo $var;while (strpos($var," ") > 0) { $var = str_replace(" "," ",$var);} How do I make this into a function?Thanks Link to comment Share on other sites More sharing options...
chibineku Posted May 11, 2010 Share Posted May 11, 2010 something like this: function strip_white_space($var) {$var = trim($var);while (strpos($var," ") > 0) { $var = str_replace(" "," ",$var);return $var;} } Link to comment Share on other sites More sharing options...
ShadowMage Posted May 11, 2010 Share Posted May 11, 2010 A regex might be more suitable if you wanted to reduce any number of whitespaces to one:$var = preg_replace('/\s+/'," ",$var); Link to comment Share on other sites More sharing options...
niche Posted May 11, 2010 Author Share Posted May 11, 2010 Where do I place the function in the script (presumably it has to be inserted before I can use it)? Link to comment Share on other sites More sharing options...
ShadowMage Posted May 11, 2010 Share Posted May 11, 2010 Where do I place the function in the script (presumably it has to be inserted before I can use it)?You can put it wherever you want it, as long as you declare it within the scope in which you need it. I typically keep my functions near the top of my scripts. Link to comment Share on other sites More sharing options...
jeffman Posted May 11, 2010 Share Posted May 11, 2010 For example function3 () { // blah}function2 () { // blah}function1 () { // blah}main() { function1(); function2(); function3();}main(); Link to comment Share on other sites More sharing options...
niche Posted May 11, 2010 Author Share Posted May 11, 2010 I understand.Many thanks to chibineku, jkloth, and Deirdre's Dad.Niche Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.