1

我想写这样的功能

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

给定刺痛包含(匹配),如何获取行格式文件(file.txt)?请帮忙

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

文件.txt

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

1 回答 1

1

以下函数将查找与您的字符串匹配的所有行,并将回显行号,如 $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);

    }

顺便说一句,不要转义字符,不要在字符串参数中使用 \,就像你在你的代码中所做的那样

阅读http://php.net/manual/en/function.strpos.php

于 2012-10-23T04:59:33.200 回答