• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

how to get data from a file

id like to make a function to get data from a file

something like:

function Retrieve_File_Data($filename,$Search){
//what to place here?
}

id like it too look for whats passed to it to look for the = sign and return whats in "" or ''
see im looking at a config file and id like to post the values for some of the config vars/
ie
$default_ccs_style = './styles/dragon1.css';

search for $default_ccs_style
returns ./styles/dragon1.css

how would one do this?
thank you in advance for any code and help you may provide
0
Johnny
Asked:
Johnny
  • 3
2 Solutions
 
psimationCommented:
Have a look at the filesystem functions in php:

http://www.php.net/manual/en/ref.filesystem.php
0
 
raja_ind82Commented:
THis is the simple method to read the file content. if you want to get the particular values from the file content, use some delimiter between the set of variables and split in the code after getting the contents from the file.

<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>

Regards,
M.Raja
0
 
JohnnyAuthor Commented:
<?

{ // Code to use if list is in a file
     $b = file("script_config.php");
}

$search="default_ccs_style"; // Search string.

echo "<pre>";

$counter=0;
$output="";

foreach ($b as $c) // For each line in the file...
      if (strpos(strtolower($c),strtolower($search))!= FALSE) // If it contains the search string...
      {
            $counter++;       // Increment the hit counter.
            $output .= "  " . trim($c) . "\n"; // Save the info for later.
      }
     
if ($counter) // If the counter was incremented (ie. we have hits)...
      echo "Your search returned $counter result(s):\n$output\n"; // Print it out.
else
      echo "No hits. =(";
     
?>

out puts
Your search returned 1 result(s):
  $default_ccs_style = './styles/dragon1.css';

now i guess the next step would be to split the string at the =

then how do we read between the "....';
to retrieve ./styles/dragon1.css as our final results?
preg?? (dang if i get how to do preg a regx stuff...i used them in vb.net before but im really bad at making thoses type of matches

hrrrmmm
this is the part i am really looking for and im not sure how to do.
0
 
JohnnyAuthor Commented:
i got it

<?
  function extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) {
    $posLeft  = stripos($inputstr,$delimeterLeft)+1;
    $posRight = stripos($inputstr,$delimeterRight,$posLeft+1);
    return  substr($inputstr,$posLeft,$posRight-$posLeft);
   }

{ // Code to use if list is in a file
     $b = file("script_config.php");
}

$search="default_ccs_style"; // Search string.

echo "<pre>";

$counter=0;
$output="";

foreach ($b as $c) // For each line in the file...
      if (strpos(strtolower($c),strtolower($search))!= FALSE) // If it contains the search string...
      {
            $counter++;       // Increment the hit counter.
            $output .= "  " . trim($c) . "\n"; // Save the info for later.
      }
     
if ($counter) // If the counter was incremented (ie. we have hits)...
      echo "Your search returned $counter result(s):\n$output\n"; // Print it out.
else
      echo "No hits. =(";
 $outputvalue = extractBetweenDelimeters($output,"'","'");
 echo  $outputvalue;
?>

ill clean the up later igota goto bed and i have a busy day tomorrow so ill clean it up on Monday sometime..

0
 
JohnnyAuthor Commented:
didn't fell you tried to FULLY answer the question.
nor did anyone comment on the fact i figured it out. so i felt you didn't try.

gave more points to raja_ind82, for partially giving an answer
{if you want to get the particular values from the file content, use some delimiter between the set of variables and split in the code after getting the contents from the file.}
wasn't that the point of the question to get the contents from the FILE???
i did not ask for my points back. I felt the question was commented on and should have been answered.
and i wanted others to see the solution.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now