Duleep
Duleep

Reputation: 587

Return given string matching line

I want write function something like this

getLine($mySteing){
 .....
}

How to get line form file(file.txt) given sting include(matching)?. please help

define('COMPANY_FULL_NAME', 'sdfstd') = getLine( "define\(\'COMPANY_FULL_NAME\'\" );

file.txt

define ( 'APP_TITLE', 'Ekdi Inc' ); 
define('COMPANY_NAME', 'WosdfP');
define('COMPANY_FULL_NAME', 'sdfstd');

Upvotes: 1

Views: 248

Answers (1)

Ionut Flavius Pogacian
Ionut Flavius Pogacian

Reputation: 4801

the following function will find all the lines that match your string, and it will echo the line number, as $i

    function getLine($string)
    {

    $file = fopen("fle.txt", "r") or exit("Unable to open file!");
    //Output a line of the file until the end is reached
    $i = 0;
    while(!feof($file))
      {
       $i++;
       $line = fgets($file);
       $pos = strpos($line, $string);
       if( $pos !== false )echo $i.'<br/>';
      }
    fclose($file);

    }

by the way, don't escape characters, don't use \ within the string parameter, as you did in yur code

read http://php.net/manual/en/function.strpos.php

Upvotes: 1

Related Questions