精确替换长文件中的字符串(句子或单词)

时间:2019-05-25 15:43:01

标签: php

我想用大文件中的代码替换句子。 我尝试使用str_replace,但是由于文件中存在句子中某些相同的单词,因此我的代码替换了 them ,但无法识别该句子!

<?php
// sentences,txt
// 12345Temple of Cheope
// ..........
// 99999Cheope

set_time_limit(0);
$GetCodice=@fopen("sentences.txt", "r");
if ($GetCodice) {
while(!feof($GetCodice)) {
    $StoreCodice=fgets($GetCodice,4096);
    $codice='".'.substr($StoreCodice, 0, 6).'."';  // abcd
    $msg=trim(substr($StoreCodice, 6));  // abcd
    echo $msg."<br>";
    $n=0;
            $file = 'longfile.php';
            replace_file($file, $msg, $codice);
   }
   fclose($GetCodice);
}

// From https://stackoverflow.com/questions/2159059/string-replace-in-a-large-file-with-php
function replace_file($path,$string, $replace)
{
    set_time_limit(0);

    if (is_file($path) === true) 
    {
        $file = fopen($path, 'r');
        $temp = tempnam('./', 'tmp');

        if (is_resource($file) === true)
        {
            while (feof($file) === false)
            {
            file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND);
            }

            fclose($file);
        }
        unlink($path);
    }
    echo $replace."<BR>";
     return rename($temp, $path);
}           
?>

句子根据其降序排列。我认为此顺序将避免替换较短的句子或较长的句子中的单词。 我希望输出

12345
.....
99999

但实际输出是

Temple of 9999
.....
99999

我可以寻求帮助吗?

提前谢谢

1 个答案:

答案 0 :(得分:1)

根据您所说的话和我对您的了解,这就是您所需要的:

set_time_limit( 0 );
//  Initialization
$inputfile           = "sentences.txt";
$outputFile          = 'longfile.php';
$matches             = array();
$extractedNumbers    = array();
$numberOfLines       = count( file( $inputfile ) );
$numberOfReadedLines = 1; // this will be used to check if the counter is on the last line or not;
//  Implementation
$GetCodice  = @fopen( $inputfile, "r" );
$newfile    = @fopen( $outputFile, 'w+' );
if ( $GetCodice ) {
    while ( ( $line = fgets( $GetCodice ) ) !== false ) {
        preg_match( '/^[0-9]+/m', $line, $matches );
        array_push( $extractedNumbers, $matches[0] );
        $position = sizeof( $extractedNumbers ) - 1;
        if ( $numberOfReadedLines == $numberOfLines ) { // if the counter is in the last line then we don't need to write a new empty line with the "\r"
            $newOutputLine = $extractedNumbers[ $position ];
        } else {
            $newOutputLine = $extractedNumbers[ $position ] . "\r";
        }
        fwrite( $newfile, $newOutputLine );
        $numberOfReadedLines++;
        //replace_file($file, $msg, $codice);

    }
    fclose( $newfile );
    fclose( $GetCodice );

}

(如果这不是您所需要的,请随时发表评论,我们可以找到一个改善措施,我只需要更多示例以进一步了解您的需求)