Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

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
SOLUTION
Avatar of psimation
psimation
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johnny

ASKER

<?

{ // 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.
Avatar of Johnny

ASKER

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..

Avatar of Johnny

ASKER

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.