[SOLVED] Find a string within a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

[SOLVED] Find a string within a file

Post 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
Last edited by tarja311 on Sat May 05, 2007 9:47 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you turn that text file into a .ini file and use parse_ini_file()?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post 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 :?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
)
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

$matches[0] returns the entire match,
$matches[1] returns the version number you want (sans the unwanted quote)
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

Thank you all for your input. It is working the way i want it now. :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Would you mind editing your original post title to add '[SOLVED] - ' to the beginning of it? Thanks.
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

Post by tarja311 »

Not at all.
Post Reply