Forum Moderators: coopster

Message Too Old, No Replies

Split - need some help

newbie to PHP in a fix

         

cochranrg

10:04 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



I'm trying to get split to work using PHP.
What I'm trying to do is read a file of users information to pull username and password for comparison to use as login. I can open the file, read it, split it into lines, but am having a problem splitting the lines into categories.
The lines read as follows, I am using a carrot "^" as separator between items and a tilde "~" for end of sentence.

First Last^login^street^city^state^zip^password~

Here is my code so far:

<?php

$login = $_POST[login];
$passwd = $_POST[passwd];
$filename = "../users.txt";
$file_contents = fopen($filename, "a+") or die("Can't Open File $filename");
$user_file = fread($file_contents, filesize($filename));

$my_users = preg_split('/~/', $user_file, -1);

foreach ($my_users as $textline) {
echo "TEXTLINE - $texline <BR>";
list($name, $login, $street, $city, $state, $zip, $password) = split('/^/',$textline);
echo "HERE TIS $name <BR>";
//$name is showing full $TEXTLINE, doesn't appear to be splitting
}

//if ($login == "") {
//header('Location: login.php');
//} else {
//setcookie ('verify', $login, 0);
//header('Location: shop.php');
//}

?>

Right now $name is returing the entire textline. It's not splitting between entries. Here is the output:

TEXTLINE -
HERE TIS Rob Cochran^rcochran^#*$! #*$!xxxx St.^xxxxxxxxx^CO^11111^passwordTEXTLINE -
HERE TIS Joe Blow^jbloe^3333 This Street^San Francisco^CA^22334^leprechaunTEXTLINE -
HERE TIS Hominy Grits^hominy^I Live Here^This City^MA^23456^gritsaregreatTEXTLINE -
HERE TIS

Any ideas? I am new to PHP, have used Perl but obviously the Split is a little different.

Thanks!

jatar_k

10:10 pm on Apr 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think it is the usage of list that is causing the prob, not sure, maybe like so

$meminfo = split('/^/',$textline);
echo "HERE TIS ", $meminfo[0], " <BR>";

cochranrg

10:14 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



Tried your suggestion, here is result:

TEXTLINE -
HERE TIS Rob Cochran^rcochran^6710 #*$! xxx xxxx.^xxxxxxx^xx^xxxx^password
TEXTLINE -
HERE TIS Joe Blow^jbloe^3333 This Street^San Francisco^CA^22334^leprechaun
TEXTLINE -
HERE TIS Hominy Grits^hominy^I Live Here^This City^MA^23456^gritsaregreat
TEXTLINE -
HERE TIS

Code:

<?php

$login = $_POST[login];
$passwd = $_POST[passwd];
$filename = "../users.txt";
$file_contents = fopen($filename, "a+") or die("Can't Open File $filename");
$user_file = fread($file_contents, filesize($filename));

$my_users = preg_split('/~/', $user_file, -1);

foreach ($my_users as $textline) {
echo "TEXTLINE - $texline <BR>";
$meminfo = split('/^/',$textline);
echo "HERE TIS $meminfo[0] <BR>";
//$name is showing full $TEXTLINE, doesn't appear to be splitting
}

//if ($login == "") {
//header('Location: login.php');
//} else {
//setcookie ('verify', $login, 0);
//header('Location: shop.php');
//}

?>

This is really wierd. Thanks anyway for the suggestion. Any more?

Thanks again,
Rob

jatar_k

10:19 pm on Apr 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$meminfo = explode [php.net]('^',$textline);
echo "HERE TIS ", $meminfo[0], " <BR>";

cochranrg

10:24 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



Ugh, same thing. Hmmm, gonna play with this some more.

I'll check back for any more ideas!

Rob

HERE TIS Rob Cochran^rcochran^#*$! xxx xxxx.^xxxxxx^xx^8xxxxpassword
HERE TIS Joe Blow^jbloe^3333 This Street^San Francisco^CA^22334^leprechaun
HERE TIS Hominy Grits^hominy^I Live Here^This City^MA^23456^gritsaregreat

jatar_k

10:26 pm on Apr 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



geez I guess I better actually test something then ;)

gimme a min

jatar_k

10:35 pm on Apr 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the explode works, tested like this

<?
$my_users = array('Rob Cochran^rcochran^#*$! #*$! xxxx.^xxxxxx^xx^8xxxxpassword',
'Joe Blow^jbloe^3333 This Street^San Francisco^CA^22334^leprechaun',
'Hominy Grits^hominy^I Live Here^This City^MA^23456^gritsaregreat');
foreach ($my_users as $textline) {
echo "<p>TEXTLINE -<BR>";
$meminfo = explode('^',$textline);
echo '<pre>';
print_r($meminfo);
echo '</pre>';
}
?>

bayview

10:38 pm on Apr 3, 2006 (gmt 0)



In a regular expression, '^' has a special meaning. It represents the beginning of a line.

To search for the literal value '^', you'll need to escape it with a backslash.

Make the relevant line look like this:
$meminfo = split('/\^/',$textline);

cochranrg

10:40 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



Very cool. That did work, but output is all of arrays. Now to figure how to compare output with $login. Thanks!

Rob

cochranrg

11:06 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



Thank you all! It is working now.

Here is the winning code:

<?php

$login = $_POST[login];
$passwd = $_POST[passwd];
$filename = "../users.txt";
$file_contents = fopen($filename, "a+") or die("Can't Open File $filename");
$user_file = fread($file_contents, filesize($filename));

$my_users = preg_split('/~/', $user_file, -1);

foreach ($my_users as $textline) {
$meminfo = explode('^',$textline);
if ($meminfo[1] == $login) {
if ($meminfo[6] == $passwd) {
echo "WE HAVE A WINNER! " . $meminfo[1] . " " . $meminfo[6];
}
}
}

?>

Rob

jatar_k

11:16 pm on Apr 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could even shorten that

this
if ($meminfo[1] == $login) {
if ($meminfo[6] == $passwd) {
echo "WE HAVE A WINNER! " . $meminfo[1] . " " . $meminfo[6];
}
}

turns into

if ($meminfo[1] == $login && $meminfo[6] == $passwd) echo 'WE HAVE A WINNER! ',$meminfo[1],' ',$meminfo[6];

inline single if
single quotes since nothing needs to be parsed for vars
commas are supported in echo so then it doesn't have to concatenate first and then echo, it does it faster

;)