Php – regex replace space with tab character


I need to replace the space between the first and second column and second and third column with a \t only.

0101 A 01/13/13
0102 F 04/05/13
0209 C 04/19/13

But i am having trouble doing this, it is putting it all in one line and writing out \t instead.

preg_replace('/(?:^|\s)/', '\t', $text);

Its printing it out as so….

\t0101\tA\t01/13/13\t\t ....

How can i properly get this in the correct format?

Any help appreciated.

Best Solution

If your data is in a strict format like this, why not

preg_replace('/\s+(.)\s+/', "\t$1\t", $text);