Gossamer Forum
Home : General : Internet Technologies :

Hw 2 use "split" (in Perl) with PHP

Quote Reply
Hw 2 use "split" (in Perl) with PHP
Hi !

Please help me to convert this PERL code to PHP

FIle: member.txt
Lisa|lisa@yahoo.com|92134|408-224-2167
Kevin|kevin@yahoo.com|92346|408-223-2292
Tom|tom@yahoo.com|97321|408-943-2547
Tim|tim@yahoo.com|93876|408-356-2604

open(FILE, "/path/to/member\.txt") ;
@array = <FILE>;
close(FILE);

foreach $line(@array) {
chomp($line);
($name, $email, $zipcode, $phone) = split(/\|/, $line);
$print_line .="Name: $name<br>Email: $email<br>Zipcode: $zipcode<br>Phone: $phone<br><br>";
}

print "Content-type: text/html\n\n";
print $print_line;
exit;

Thanks !

PS: I'm learning PHP by convert Perl code to PHP code
Quote Reply
Re: [newage24] Hw 2 use "split" (in Perl) with PHP In reply to
Well rather than have someone just hand you the answer, how bout showing us what you've attempted so far, and what your results have been.

You'll learn more by attempting and debugging, than you will by being handed code every time.

--mark
Quote Reply
Re: [newage24] Hw 2 use "split" (in Perl) with PHP In reply to
I've never used php so I'm going to have an attempt for fun, let's see what Mark thinks =)

Code:
<?php

$fh = fopen("/path/to/member.txt", "r");

if (is_resource($fh)) {
while (! feof($fh)) {
$buffer = fgets($fh, 4096);
$array = explode("|", $buffer);
echo "Name: $array[0]<br>Email: $array[1]<br>Zip: $array[2]<br>Phone: $array[3]\n";
}
fclose($fh);
}

?>
Quote Reply
Re: [Paul] Hw 2 use "split" (in Perl) with PHP In reply to
Not bad Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!