Solved

convert columns to tab separated lines

Posted on 2004-07-30
4
211 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
4 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 125 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 125 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

823 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