Jump to content

[SOLVED] REGEX Problem.


John_S

Recommended Posts

Hello everybody,

I have a little problem with a small part of code:

$users = file("users.db.php");
foreach ($users as $line) {

    if(!eregi("<\?php",$line)) {
    $user = explode('|', $line);
    echo $user[0]."\n".$user[1]."\n".$user[2]."\n".$user[3]."\n".$user[4];
    }
    
}

 

I am trying to get out all lines from a file except all lines that start with <?php, the problem is... it doesn't work, it doesn't print anything at all.

 

In that file (users.db.php) I have the following code:

<?php die("Access Restricted. You are not allowed to access this file"); ?>
Id|Username|Password|Permissions|Last Logged In|Email|Registered|

 

Could somebody tell me please where is my mistake? Thanks a lot in advance!  :)

Link to comment
https://forums.phpfreaks.com/topic/148113-solved-regex-problem/
Share on other sites

Your code works fine for me. I used the code you provided and created a test.php file, then created a (users.db.php) file with the content you showed.

 

The output I received when running the test php file is

Id
Username
Password
Permissions
Last Logged In

 

You should get an error if the file name or path are incorrect. Are you poistive you don't have two versions of users.db.php floating around? Try running just this code to ensure you are reading the file

$users = file("users.db.php");
$lineno = 1;
foreach ($users as $line)
{
    $user = explode('|', $line);
    echo "Line $lineno: $line\n";
$lineno++;
}

Link to comment
https://forums.phpfreaks.com/topic/148113-solved-regex-problem/#findComment-777484
Share on other sites

  Quote

Your code works fine for me. I used the code you provided and created a test.php file, then created a (users.db.php) file with the content you showed.

 

The output I received when running the test php file is

Id
Username
Password
Permissions
Last Logged In

 

You should get an error if the file name or path are incorrect. Are you poistive you don't have two versions of users.db.php floating around? Try running just this code to ensure you are reading the file

$users = file("users.db.php");
$lineno = 1;
foreach ($users as $line)
{
    $user = explode('|', $line);
    echo "Line $lineno: $line ";
   $lineno++;
}

 

Hello there,

 

When I run your code, I receive this :

Line 1: <?php die("Access Restricted. You are not allowed to access this file"); ?>
Id|Username|Password|Permissions|Last Logged In|Email|Registered|

 

Seems weird to me actually. And when myself I execute my script I got a blank browser window => no output at all. Might it be because the content is "technically" on the same line? (In my editor I see it on two lines but it might be on one if there isn't a proper  )

 

EDIT: Thanks a lot! Your part of code made me realize that two pices <?php... and id|username... were "technically" on the same line since I had "\n"s everywhere but not "\r"s.

 

So even thought I saw the code in my editor being on two different lines, it wasn't like that in the file itself.

Link to comment
https://forums.phpfreaks.com/topic/148113-solved-regex-problem/#findComment-777492
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.