ok im in desperate need of getting this done. i have the following text file:

[Users]
Jack= Test A
Pete=Test B
Rose=test c

[Users2]
Jack2=testpp
Pete2=Test B
Rose2=test c

and heres my php file (not the it parses it through the url):

<?php
$fp=fopen("$file","rb");
$contents=fread($fp,filesize($file));
$explode=explode("$section]", $contents);
echo $explode[1];
$pregit=preg_match("/$key=./",@$explode[1],$preg);
echo $pregit;
$explode=explode("=", $preg[0]);
if($explode[1]=='')
{
echo $default;
}else
{
echo $explode[1];
}
?>

ok here is the example of the url:

http://localhost/profile/ps.php?file=test.txt&section=Users2&key=Jack&default=ee

now here's what happens in the text file it should match Jack but it there is no jack in the section it is looking in so it should return default but instead it is using jack2. any ideas? all help is appreciated..

    Perhaps one could try not even bothering with a regex?

    <?
    //you have an ini_file format, use it
    $sections = parse_ini_file($file,true);

    //does user '$key' exist?
    if (($data = @$sections[$section][$key])<1) {
    //no... so use the default...
    $data = $default;
    }

    echo $data;

    ?>
    ...seems like it should work

      side note:
      Passing file names via a URL is a realy bad idea. So tempting for a user to want to test out other file names if you know what I mean...

      Perhaps you should make up little names and not use the var $file?

      Try ?ref=sects

      and in your code...
      if (isset($ref) && $ref == 'sects') {
      $file = 'file.txt';
      } else {
      exit();
      //or better still be graceful
      //by generating a page and giving a custom error.
      }

        well i pass it throught the url cuz thats how my client wanted it thanks though for the parse_ini...saved me a bunch

          Perhaps you should present the point to your client, if it's a page that will be in a public area, there is a strong case for some concern.

          But, I'll leave that to you of course (like I have a choice :] )

          Glad you got what you needed.
          Enjoy.

            Write a Reply...