?
Solved

rearrange words: line to list

Posted on 2012-08-17
12
Medium Priority
?
352 Views
Last Modified: 2012-08-17
Hi experts,

I have a .txt file with "email,firstname,lastname" info and need to do this for every line:

(User1)
Email:email
Website1:website1name.com
Website2:website2name.com
Website3:website3name.com
Website4:website4name.com
FName:firstname
LName:lastname
(User2)
Email:email
Website1:website1name.com
Website2:website2name.com
Website3:website3name.com
Website4:website4name.com
FName:firstname
LName:lastname

and so on for hundreds of users (lines).

Thanks.
0
Comment
Question by:gloriaewold41
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38304614
You can try this not tested code:
 
$lines = array();
$list = array();
$lines = file("yourTextFile.txt");
foreach ($lines as $line) {
  // for output in a new file
  $list[] = str_replace(',', '\n', $line);
  // if you need html format
  $list[] = str_replace(',', '<br />', $line);
}
foreach ($list as $listItem) {
  echo $listItem . "<br />;
}

Cheers
0
 

Author Comment

by:gloriaewold41
ID: 38304637
What is that marqusG ?
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38304694
Without knowing exactly what your input and output format is (you seem to have a variable output on number of fields).
Here is something you can work with.
$fp = fopen("info.txt", "rt");
while($line = fgets($fp)) {
   $items = explode(',', $line);
   echo "Email: " . $items[0] . "\n";
   echo "WebsiteEmail: " . $items[0] . "\n";
   ...
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:gloriaewold41
ID: 38304768
.txt is the format for both input and output. please help i'm not so good at scripting, this one is too advanced for me.
0
 
LVL 45

Expert Comment

by:Chris Stanyon
ID: 38304772
Where is website1, website2 etc coming from?

What do you need to do with the info: output it to screen - echo, output to another text file etc.?

Not really enough info in your question to give accurate answers!
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38304778
"That" is the code you have to use to get the output you want from a text file. With that code, you process the file and print to the screen the output as a list.
Obviously, the output will contain only the data found in the file, so Website1, Website2 and so on are not present in the ouput provided by my code. If you'll be more clear about what is the input, the output and the intermediate steps, I'll can be more precise.

Cheers
0
 

Author Comment

by:gloriaewold41
ID: 38304795
i need to add websites manually, not from another file, like

$website1 = "website1.com"

and output on screen, it's easier.
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38304801
@gloriaewold41 to properly assist you we need to know exactly what the data in the text file looks like (all variations) and exactly how that data needs to be out put.

For instance you have in your example above have 4 website addresses listed in the output but you don't say where these are in the input - if the input has all 4 - if you need to cater for more than 4 etc.

Does the input make provision for each field required with blanks where data is not provided - if not how will the script know what field belongs where.

These are the sort of questions we need answerd to help you.

Perhaps post a copy of your text file - with say the first 3 or 4 lines.

In terms of your output - do you have a specification for exactly what that will be - failing that can you tell us how the output will be used.

The answers to the above will help us give you a solution that matches your requirement.
0
 

Author Comment

by:gloriaewold41
ID: 38304809
input file looks exactly like this:

email,firstname,lastname
email,firstname,lastname
email,firstname,lastname
etc

and need to output this

(User1)
Email:email
Website1:website1name.com
Website2:website2name.com
Website3:website3name.com
Website4:website4name.com
FName:firstname
LName:lastname
(User2)
Email:email
Website1:website1name.com
Website2:website2name.com
Website3:website3name.com
Website4:website4name.com
FName:firstname
LName:lastname
(User3)
Email:email
Website1:website1name.com
Website2:website2name.com
Website3:website3name.com
Website4:website4name.com
FName:firstname
LName:lastname
0
 
LVL 45

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 38304830
Have a look at this and ask if you have any questions.

<?php 
//file to read from
$lines = file('formatfileinput.txt');

//file to write to
$output = fopen("formatfileoutput.txt", 'w') or die("can't open file");

//set a counter for user
$i = 1;

foreach ($lines as $line) {
//split the string
$info = explode(",", $line);

//create the new string
$stringData = <<<EOT
(User$i)
Email:$info[0]
Website1:website1name.com
Website2:website2name.com
Website3:website3name.com
Website4:website4name.com
FName:$info[1]
LName:$info[2]
EOT;

//write to the file
fwrite($output, $stringData);

//increase the counter
$i++;
}


//close the file
fclose($output);
?>

Open in new window

0
 

Author Closing Comment

by:gloriaewold41
ID: 38304862
This is great. Thanks
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38304898
$fp = fopen("info.txt", "rt");
$i=1;
while($line = fgets($fp)) {
   $items = explode(',', $line);
   echo "(User" . $i++ . ")\n"
   echo "Email: " . $items[0] . "\n";
   echo "WebsiteEmail:website1name.com\n";
   echo "WebsiteEmail:website2name.com\n";
   echo "WebsiteEmail:website3name.com\n";
   echo "WebsiteEmail:website4name.com\n";
   echo "FName:" . $items[1] . "\n";
   echo "LName:" . $items[1] . "\n";
}
fclose($fp);

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 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