Link to home
Start Free TrialLog in
Avatar of gloriaewold41
gloriaewold41

asked on

rearrange words: line to list

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.
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

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
Avatar of gloriaewold41
gloriaewold41

ASKER

What is that marqusG ?
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

.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.
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!
"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
i need to add websites manually, not from another file, like

$website1 = "website1.com"

and output on screen, it's easier.
@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.
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
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is great. Thanks
$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