Page 1 of 1

[SOLVED] Find a string within a file

Posted: Fri May 04, 2007 4:28 pm
by tarja311
Hi guys. I am trying to find the string "version" within a textfile, and whatever that string is equaled to, print that part to the screen.

So far my code looks like this :

Code: Select all

$contents = file_get_contents('somefile.txt');
$string = "version";

if(strstr($contents, $string))
	echo "found it.";
else
	echo "not found.";
The string is found, but i do not know where to go from here. The text file looks something like this :

application = "app"
size = "400k"
version = "1.0"


I just want to print out the 1.0 part of it. Any ideas?

Thanks

--tarja

Posted: Fri May 04, 2007 5:03 pm
by RobertGonzalez
Can you turn that text file into a .ini file and use parse_ini_file()?

Posted: Fri May 04, 2007 5:06 pm
by toasty2
Here is a working (but probably bad) way to do it:

Code: Select all

$result = explode($string.' = "', $contents);
$result = explode('"', $result[1]);
$result = $result[0];
It returns 1.0
Here it is as a function:

Code: Select all

function getstring($string,$contents)
{
$result = explode($string.' = "', $contents);
$result = explode('"', $result[1]);
$result = $result[0];
}
Edit: I was writing that code while Everah wrote his reply, you should consider his idea.

Posted: Fri May 04, 2007 6:32 pm
by tarja311
Thanks guys.
Everah wrote:Can you turn that text file into a .ini file and use parse_ini_file()?
Unfortunately i cant. :?

@GWsux : Thanks, that worked, but with one problem. If there is an extra space or a tab between $string and = the code will not work. I have tried rtrim such as :

Code: Select all

$string = rtrim($string);
.. with no luck.

This text file can be modifed by any of my users. I don't want an extra blank space or tab to break my code :?

Posted: Fri May 04, 2007 7:01 pm
by John Cartwright
Regex to the rescue!

Code: Select all

$test = '
application = "app"
size = "400k"
version =                      "1.0"';

preg_match('#version\s+=\s+"(\d{1,2}\.\d{1,2})#', $test, $matches);

echo '<pre>';
print_r($matches);
Returns:

Code: Select all

Array
(
    [0] => version             =               "1.0
    [1] => 1.0
)

Posted: Fri May 04, 2007 10:15 pm
by toasty2
Jcart's method is the best, I would've suggested something similar, but I don't know how to do regular expressions. :(

Jcart, there is one problem with yours, it leaves an unwanted quotation mark.

Posted: Fri May 04, 2007 10:27 pm
by John Cartwright
$matches[0] returns the entire match,
$matches[1] returns the version number you want (sans the unwanted quote)

Posted: Sat May 05, 2007 5:14 am
by neel_basu
OK So this function is what you need
Use this Function

Code: Select all

function ret_ini($file)
{
  $str =file_get_contents($file);
  $r = explode("\n", $str);
  $i = 0;
  foreach($r as $val)
    {
      preg_match_all('/(.*) ?= ?["?](.*)["?]/', $val, $matches[$i]);
      foreach($matches[$i] as $key => $value)
        {
          if(is_array($value))
            {
              foreach($value as $sub_key => $sub_val)
                {
                  $matches[$i][$key][$sub_key] = trim($sub_val);
                }
            }
          else
            {
              $matches[$i][$key] = trim($value);
            }
        }
      $i++;
    }
    for($i = 0;$i<= count($matches)-1;$i++)
      {
        $ret[$matches[$i][1][0]] = $matches[$i][2][0];
      }
    return $ret;
  }
Usage

Code: Select all

$ret = ret_ini('ini.ini');//Your ini file in ret_ini('you_ini_file.ini')
print_r($ret);//The Function returns Array
Output wrote:Array
(
[application] => app
[size] => 400k
[version] => 1.0
)
This function will output your compleate ini settings in an Array

Posted: Sat May 05, 2007 11:55 am
by tarja311
Thank you all for your input. It is working the way i want it now. :)

Posted: Sat May 05, 2007 4:24 pm
by RobertGonzalez
Would you mind editing your original post title to add '[SOLVED] - ' to the beginning of it? Thanks.

Posted: Sat May 05, 2007 9:46 pm
by tarja311
Not at all.