【问题标题】:Exact replacing a string (a sentence or a word) in a long file精确替换长文件中的字符串(句子或单词)
【发布时间】:2019-05-26 19:54:17
【问题描述】:

我想用大文件中的代码替换句子。 我曾尝试使用 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

我能得到一些帮助吗?

提前谢谢你

【问题讨论】:

  • 你好,“一个句子”是什么意思?你的意思是“线”?如果是,请告诉我您是想用您的代码替换一行还是所有行?
  • 我的意思是一个句子(“我爱玛丽”、“她不爱我”等),一行可以是很多句子或代码。
  • 您的意思是要替换例如:12345Temple 12345Temple 12345Temple 99999Cheope 吗? :12345 12345 12345 99999?
  • 我想用“9999”替换每次出现的“Cheope”,用“12345”替换“Temple of Cheope”。使用我的代码,我得到“99999”和“99999 神殿”!我必须使用 PHP

标签: php


【解决方案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 );

}

(如果这不是您需要的,请随时发表评论,我们可以找到改进之处,我只需要更多示例来了解您的需求)

【讨论】:

  • 非常感谢。不幸的是,它不起作用。我不明白输入文件(sentences.txt)中的句子在哪里被搜索并在输出文件(longfile.php)的行中被替换。我在以下行出现错误:array_push($extractedNumbers, $matches[0]);
  • 不客气,对我来说它就像一个魅力,所以你能在这里传递错误信息吗?解释我所做的:使用“preg_match”函数(它是一个正则表达式函数)在文件中的每一行搜索输入文件的句子。我用它从你的当前提取数字并将其放入$matches 数组而不是将其附加到输出文件
  • 您好,这是错误消息:注意:未定义的偏移量:0 in ....\trovasost2.php 第 16 行(即 array_push( $extractedNumbers, $matches[0] ); 和长文件为空!
  • 您没有指定您的输入文件是否可以包含没有代码的句子?该错误表示有一行(可能是第16行)不包含代码,但它只包含字母
猜你喜欢
  • 2019-03-11
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
相关资源
最近更新 更多