繁体   English   中英

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

[英]Exact replacing a string (a sentence or a word) in a long file

我想用大文件中的代码替换句子。 我尝试使用str_replace,但是因为在文件中存在句子中某些相同的词,所以我的代码替换了它们并且无法识别该句子!

<?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

我可以帮忙吗?

先感谢您

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

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 );

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM