nathalie_vandv Posted February 17, 2007 Share Posted February 17, 2007 I have a flat text database where I want to extract values. For example, the txt file looks like "a=1 b=2 c=3 d=4". Is there a predefined function like get('b') that return 2 or how can it be done? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted February 17, 2007 Share Posted February 17, 2007 I don't think there is such a function that exists. However it is rather simple to create. Here's what I've made: <?php function getVar($gVar) { $txtFileContents = "a=1 b=2 c=3 d=4"; // explode each variable in the file into an array on their own. $vars = explode(" ", $txtFileContents); /* produces an array like the following: array ( [0] = "a=1" [1] = "b=2" ... } */ // now we loop through each item in the $vars array. foreach($vars as $var) { // again we use explode and then we create two new variables // $name and $value. list($name, $value) = explode("=", $var); // we now create a new array which contains all // the variables in the file called $fileVars $fileVars[$name] = $value; } // now we check whether the variable that was passed to this // function exits in the $fileVars array and echo out the results if(array_key_exists($gVar, $fileVars)) { echo $fileVars[$gVar]; } else { echo $gVar . ' doesn\'t exist'; } } getVar('c'); // get variable c -- returns "3" // getVar('e'); // get variable e -- returns "e doesn't exist" ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.