dreamci Posted January 13, 2009 Share Posted January 13, 2009 hello i am trying to create a csv file with php so i need to put these values in to same line , i have to remove the spaces but trim function doesnt work can someone help me how to clear the spaces and put them in same line 11, Rue Etienne Oehmichen 51100 REIMS France Telephone : +33 3 26 82 61 82 Fax : +33 3 26 82 86 15 Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/ Share on other sites More sharing options...
cwarn23 Posted January 13, 2009 Share Posted January 13, 2009 I have just made a script for you and it basically removes the spaces before text and after the text. It also removes the lines. And it is as follows: <? $string=' 11, Rue Etienne Oehmichen 51100 REIMS France Telephone : +33 3 26 82 61 82 Fax : +33 3 26 82 86 15'; //above is the variable assigned the value //below is the process to remove the spaces and lines. while (preg_replace('/( ( )| ( )| ( )|( ) )/is','$2',$string)!==$string) { $string=preg_replace('/( ( )| ( )| ( )|( ) )/is','$2',$string); } while (preg_replace('/([ ]+)(.*)([ ])( )/is','$2',$string)!==$string) //new line needed. { $string=preg_replace('/([ ]+)(.*)([ ])( )/is','$2',$string); //new line needed. } while (preg_replace('/([ ]+)?(.*)([ ])( )/is','$2',$string)!==$string) //new line needed. { $string=preg_replace('/([ ]+)?(.*)([ ])( )/is','$2',$string); //new line needed. } while (preg_replace('/([ ]+)(.*)([ ])?( )/is','$2',$string)!==$string) //new line needed. { $string=preg_replace('/([ ]+)(.*)([ ])?( )/is','$2',$string); //new line needed. } $string=preg_replace('/[ ]+(.*)/is','$1',$string); //new line needed. // above was to process to remove the spaces and lines. // below displays the new string. echo $string; ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/#findComment-736042 Share on other sites More sharing options...
nrg_alpha Posted January 13, 2009 Share Posted January 13, 2009 Unless I am misunderstanding something: $str=' 11, Rue Etienne Oehmichen 51100 REIMS France Telephone : +33 3 26 82 61 82 Fax : +33 3 26 82 86 15 '; $str = preg_replace('#[\t\r\n]#', '', $str); $str = trim(preg_replace('# {2,}#', ' ', $str), ' '); echo $str; Output: 11, Rue Etienne Oehmichen 51100 REIMS France Telephone : +33 3 26 82 61 82 Fax : +33 3 26 82 86 15 To see if the above code snippet works, we can test for any oddball spaces by using this echo line instead: echo str_replace(' ', '~', $str); Output: 11,~Rue~Etienne~Oehmichen~51100~REIMS~France~Telephone~:~+33~3~26~82~61~82~Fax~:~+33~3~26~82~86~15 As you can see, there is no initial nor trailing spaces.. everything is on one line, with a single space separating each element. If I have misunderstood things, then my apologies in advance. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/#findComment-736079 Share on other sites More sharing options...
nrg_alpha Posted January 13, 2009 Share Posted January 13, 2009 This is why it's bad to code sooo late into the morning.. the same results could be acheived simply by doing: $str=' 11, Rue Etienne Oehmichen 51100 REIMS France Telephone : +33 3 26 82 61 82 Fax : +33 3 26 82 86 15 '; $str = trim(preg_replace('#\s{2,}#', ' ', $str), ' '); echo $str; Same output as above... once again, to test and see if there are oddball spaces, simply output with the alternative echo line listed last time. Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/#findComment-736085 Share on other sites More sharing options...
effigy Posted January 13, 2009 Share Posted January 13, 2009 $pieces = preg_split('/\s{2,}/', $data, -1, PREG_SPLIT_NO_EMPTY); echo join(',', $pieces); Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/#findComment-736255 Share on other sites More sharing options...
nrg_alpha Posted January 13, 2009 Share Posted January 13, 2009 $pieces = preg_split('/\s{2,}/', $data, -1, PREG_SPLIT_NO_EMPTY); echo join(',', $pieces); Yep, that'll do it too... faster even! In the last words of a final chess move, 'Checkmate'! Just out of curiosity, I occasionally see functions like say join (which are in turn simply an alias for implode [or chop vs rtrim] as another example)..Why does PHP have some functions that have alternative aliases? Wouldn't it be simpler to only have one function (in this case) only join or only implode? I once read somewhere that the only difference is the entry of a hash table, therefore, only the function name is different, not what is inside it (not sure if there is any truth to that or not). Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/#findComment-736573 Share on other sites More sharing options...
cwarn23 Posted January 14, 2009 Share Posted January 14, 2009 $pieces = preg_split('/\s{2,}/', $data, -1, PREG_SPLIT_NO_EMPTY); echo join(',', $pieces); Yep, that'll do it too... faster even! In the last words of a final chess move, 'Checkmate'! Just out of curiosity, I occasionally see functions like say join (which are in turn simply an alias for implode [or chop vs rtrim] as another example)..Why does PHP have some functions that have alternative aliases? Wouldn't it be simpler to only have one function (in this case) only join or only implode? I once read somewhere that the only difference is the entry of a hash table, therefore, only the function name is different, not what is inside it (not sure if there is any truth to that or not). I believe the answer to that question is speed and performance vs easy to use if I am reading that correctly. Because when you think about it, the more a function holds, the longer it takes for the function to execute. That is why you never use preg_split() when you can use explode() or you never use preg_replace() when you can use str_replace(). So it makes sense to have lots of little functions. That is why php is so fast (as they claim) and reliable. Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/#findComment-736774 Share on other sites More sharing options...
nrg_alpha Posted January 14, 2009 Share Posted January 14, 2009 I believe the answer to that question is speed and performance vs easy to use if I am reading that correctly. Because when you think about it, the more a function holds, the longer it takes for the function to execute. But if a function in one is faster than the other, yet both offer the exact same fucntionality, flags and such (example, sizeof() and count(), both support the additional flag to count a multi-dimensional array), why not just take the most effective version and only keep it named as the orginal (presumably, sizeof in this case)? That is why you never use preg_split() when you can use explode() or you never use preg_replace() when you can use str_replace(). So it makes sense to have lots of little functions. That is why php is so fast (as they claim) and reliable. Well, this is a bad example, as you are comparing apples to oranges.. it is evident that preg_split and explode (while they can perform the same thing under certain circumstances), doesn't necessarily do the exact same thing. We know that preg_split can make use of more complex regex patterns as a way to split something up (where as explode doesn't). But what I am referring to are aliases specifically. preg_split is NOT an alias of explode, where as [as mentioned above], sizeof() IS an alias of count().. so this all links back to me wondering about join vs implode... just seems redundant. Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/#findComment-736785 Share on other sites More sharing options...
effigy Posted January 14, 2009 Share Posted January 14, 2009 Why does PHP have some functions that have alternative aliases? My guess is to accommodate those coming from other languages. For example, Perl uses join rather than implode, so I'd much rather use join across the board than remember the idiosyncrasies. Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/#findComment-737009 Share on other sites More sharing options...
nrg_alpha Posted January 14, 2009 Share Posted January 14, 2009 Why does PHP have some functions that have alternative aliases? My guess is to accommodate those coming from other languages. For example, Perl uses join rather than implode, so I'd much rather use join across the board than remember the idiosyncrasies. Ah, that makes sense (although still redundant IMO). Could help ease the transition so to speak. Quote Link to comment https://forums.phpfreaks.com/topic/140647-hello-need-help-about-replacing-spaces/#findComment-737294 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.