Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.