Solved

convert columns to tab separated lines

Posted on 2004-07-30
4
193 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now