We help IT Professionals succeed at work.

Table from text file

pauledwardian
on
446 Views
Last Modified: 2012-02-10
I have a textfile that has information such as Customer name, email, phone number
There are exactlty 5 spaces between each world in each line.
for example:

Paul     paul@hotmail.com     9856651123
John     John@hotmail.com     5654445523

How can I use a php code that would create a table that would have only 3 column and enough number of rows that covers the number of lines I have in my text file?

Paul
Comment
Watch Question

Commented:
Try this:
<?php
$data = <<<LINES
Paul     paul@hotmail.com     9856651123
John     John@hotmail.com     5654445523
LINES;

// \x20 is a space represented in hexadecimal string
preg_match_all('/^(.*)\x20{5}(.*)\x20{5}(.*)$/m', $data, $m, PREG_SET_ORDER);

echo "<table>";
foreach ($m as $line) {
	echo "<tr><td>{$line[1]}</td><td>{$line[2]}</td><td>{$line[3]}</td></tr>";
}
echo "</table>";
?>

Open in new window

Author

Commented:
so where should I specify the name of my text file?

Commented:
Check this:

<?php
$data = file_get_contents(YOUR_TEXT_FILE_NAME);

// \x20 is a space represented in hexadecimal string
preg_match_all('/^(.*)\x20{5}(.*)\x20{5}(.*)$/m', $data, $m, PREG_SET_ORDER);

echo "<table>";
foreach ($m as $line) {
	echo "<tr><td>{$line[1]}</td><td>{$line[2]}</td><td>{$line[3]}</td></tr>";
}
echo "</table>";
?>

Open in new window

Author

Commented:
Thanks but I only see the textfile content now. There is not table
Its only like plain text like this:

Paul paul@hotmail.com 9856651123  
John John@hotmail.com 5654445523

Its not inside the table
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks a lot!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.