Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

convert columns to tab separated lines

Posted on 2004-07-30
4
Medium Priority
?
249 Views
Last Modified: 2006-11-17
I have in a file a column.
**********
red
green
blue
yellow
**********
black
purple
violet
brown
gold
**********



I want to move this to a new file and tab separate lines.

red \t green \t blue \t yellow
black \t purple \t violet \t brown \t gold

Moving to the new file I can do.
How do I make a tab separated lines and omit the ********** ??
0
Comment
Question by:Ted22
2 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 11682502
For the purpose of example i have put the original data you provided into a variable:

$var = "
**********
red
green
blue
yellow
**********
black
purple
violet
brown
gold
**********
";

Theres two main ways you can tackle this, one is string replacement, eg.

//replace new lines with tabs and strip out *
$var = str_replace("\n","\t",$var);
$var = str_replace("*","",$var);

The second way, and the one i would use, is to use a simple preg_split to procuce nice tidy output

$var = preg_split("/[\s\*]+/",$var,-1,PREG_SPLIT_NO_EMPTY);
$var = implode(" \t ",$var);

use echo $var; to test the output if needed

Diablo
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 500 total points
ID: 11690655
About Diablo84 first method, doesn't work as expected because you are eliminating ALL newlines. It would be better:

$var = str_replace("\n","\t",$var);    // Replace all newlines by tabs
$var = str_replace("**********","\n",$var);    // Replace asterisks separator with a newline

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question