Jump to content

whitespace


Destramic

Recommended Posts

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);
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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?

 

 

 

 

Link to comment
Share on other sites

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   

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.