eiledon01 Posted April 9, 2009 Share Posted April 9, 2009 Sounds simple, and probably is. I need to replace something like: "test 1 sentence" with "test 1[tab]sentence" The number of spaces between columns varies from 3 to 9 I've tried eregi_replace("\s{3,9}+","\t",$str) with no luck; any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/153375-solved-need-to-replace-spaces-with-tabs/ Share on other sites More sharing options...
nrg_alpha Posted April 9, 2009 Share Posted April 9, 2009 Use preg_replace instead, as ereg (POSIX) will be in the core of PHP6 by default. So futureproof your code now by learning preg. As for the pattern, you almost got it..if you want to only replace 3 to 9 consecutive spaces, you can use something like: $str = preg_replace('#\s{3,9}#', "\t", $str); // where $str represents your string in question (You don't need the + quantifier after the {3,9} part). EDIT - You can learn about regex from the following sources: - weblogtoolscollection - PHPfreak resources - Book Mastering Regular Expressions - Regular-expressions.info Quote Link to comment https://forums.phpfreaks.com/topic/153375-solved-need-to-replace-spaces-with-tabs/#findComment-805798 Share on other sites More sharing options...
eiledon01 Posted April 9, 2009 Author Share Posted April 9, 2009 working now, thanks Quote Link to comment https://forums.phpfreaks.com/topic/153375-solved-need-to-replace-spaces-with-tabs/#findComment-805819 Share on other sites More sharing options...
.josh Posted April 10, 2009 Share Posted April 10, 2009 Just to clarify what nrg said, + is a quantifier. It means 1 or more of something. You don't need it because {3,9} is the quantifier, meaning 3 to 9 of something. Quote Link to comment https://forums.phpfreaks.com/topic/153375-solved-need-to-replace-spaces-with-tabs/#findComment-806046 Share on other sites More sharing options...
nrg_alpha Posted April 10, 2009 Share Posted April 10, 2009 @OP: Sorry I didn't elaborate on the quantifier thing. Also note that when I said: Use preg_replace instead, as ereg (POSIX) will be in the core of PHP6 by default. What I should have said is: "Use preg_replace instead (part of PCRE - Perl Compatible Regular Expressions), as ereg (POSIX) will NOT be in the core of PHP6." Quote Link to comment https://forums.phpfreaks.com/topic/153375-solved-need-to-replace-spaces-with-tabs/#findComment-806062 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.