Jump to content

[SOLVED] Need to replace Spaces with Tabs


eiledon01

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/153375-solved-need-to-replace-spaces-with-tabs/
Share on other sites

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

@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."

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.