Destramic Posted April 24, 2011 Share Posted April 24, 2011 hey guys this script below will load a document file...txt, ini, ctf etc...but if the file has a new line from (whitespace) its not finding it...im using if (preg_match('\S', $i) == false) if that is the correct way to find a new line/white space within a txt document if someone could tell me what im doing wrong please function read($file_name) { $this->_file_name = $file_name; $file = fopen($this->_file_name, 'r'); while ($i = fgets($file)) { if (preg_match('\S', $i) == false) { preg_match('/^(.*?)=(.*?)$/', $i, $found); $this->_values[$found[1]] = $found[2]; } } fclose($file); print_r($this->_values); } Quote Link to comment Share on other sites More sharing options...
Destramic Posted April 25, 2011 Author Share Posted April 25, 2011 basically my test file looks like this and the break is being read by my loop if anyone can please help host = localhost username = destramic password = test database = test ; this space here is being read by my loop developement = true Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted April 25, 2011 Share Posted April 25, 2011 Maybe use file instead. File load each line into an array, Eg $lines = file('file.txt'); $lines[0] will be line1 $lines[1] will be line 2 etc. You can loop through the lines with a foreach loop $lines = file($this->_file_name); foreach($lines as $line) { // skip lines with spaces if(trim($line) == '') continue; preg_match('/^(.*?)=(.*?)$/', $line, $found); $this->_values[$found[1]] = $found[2]; print_r($this->_values); } Quote Link to comment Share on other sites More sharing options...
Destramic Posted April 25, 2011 Author Share Posted April 25, 2011 thanks wildteen88 i got it working now with if (!preg_match('/^\s*$/', $i)) now the one other problem i have with the script is that this line preg_match('/^(.*?)=(.*?)$/', $i, $found); will read spaces so say for instance this line in the .cfg file is like host = localhost then it will read the white spaces if they can be removed? Quote Link to comment Share on other sites More sharing options...
Destramic Posted April 25, 2011 Author Share Posted April 25, 2011 this is the output of the array....you can see the whitespaces [developement_enviroment ] => true [mysql_host ] => localhost [mysql_username ] => root [mysql_password ] => test [mysql_database_name ] => test [autoload_ignore_directorys[] ] => .htaccess Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted April 25, 2011 Share Posted April 25, 2011 use trim on $found[1] variable. Quote Link to comment Share on other sites More sharing options...
Destramic Posted April 25, 2011 Author Share Posted April 25, 2011 thanks for you help 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.