Solved

convert columns to tab separated lines

Posted on 2004-07-30
4
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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