【问题标题】:PHP - Find/Replace Text in RTF/txt filesPHP - 在 RTF/txt 文件中查找/替换文本
【发布时间】:2017-01-21 14:44:41
【问题描述】:

我在查找特定文本并将其替换为替代文本时遇到了问题。 我在下面仅使用 .rtf.txt 文件测试我的代码。我还确保文件在我的服务器中是可写的。

这是一个偶然的情况,我很好奇我的代码是否错误,或者这只是打开和操作文件的怪异。

<?php

$filelocation = '/tmp/demo.txt';
$firstname = 'John';
$lastname = 'Smith';

$output = file_get_contents($filelocation);
$output = str_replace('[[FIRSTNAME]]', $firstname, $output);
$output = str_replace('[[LASTNAME]]', $lastname, $output);
$output = str_replace('[[TODAY]]', date('F j, Y'), $output);

// rewrite file
file_put_contents($filelocation, $output);

?>

所以,在 demo.txt 文件中,我有大约一整页的文本,其中散布着 [[FIRSTNAME]]、[[LASTNAME]] 和 [[TODAY]]。

查找/替换时时好时坏。到目前为止,[[TODAY]] 总是被正确替换,而名称却不是。

有人遇到过同样的问题吗?

(顺便说一句,我检查了错误日志,到目前为止,打开文件或写入文件都没有返回 PHP 警告/错误)

【问题讨论】:

  • 你能在 demo.txt 里面放一些文本,这样我们就可以知道哪里出了问题
  • 好点,我会试试....
  • 奇怪 - 用一个简单的文本文件尝试了您的代码,它可以按预期工作。不知道你的东西发生了什么。发布演示文件?
  • 让我看看我是否明白这一点。您想用John 替换[[FIRSTNAME]] 和用Smith 替换[[LASTNAME]]
  • 没错,这是一个查找和替换方案。

标签: php str-replace


【解决方案1】:

如果没有看到 demo.txt 的内容,很难确定。我的第一个猜测是使用括号作为指针可能会出现问题。我会尝试更改为 RTF 不使用的东西,例如百分号或星号。例如:%%FIRSTNAME%%, **FIRSTNAME**(当然这是假设您可以控制 demo.txt 的内容。)

【讨论】:

  • 看来这确实是一个.rtf 问题。我已经用.txt 对其进行了测试,它在那里运行良好。也许 Microsoft Word 产生了奇怪的隐形格式添加剂。
  • 是的,RTF 添加了所有类型的格式化字符。如果我的回答有帮助,请投票或标记为答案。谢谢!
  • 你是对的。我需要投票。我用你的想法尝试了不同的 Pre/Post 标记参数。另外,我从.rtf 中删除了所有格式,这似乎是一个解决方案。富文本万岁,有点。
  • 我遇到过几次。很高兴我能帮忙;)
【解决方案2】:

我也遇到过这个问题。似乎 Microsoft Word 在标签中插入了格式代码。我在我的技术博客上发表了一篇关于如何解决这个问题的博文。

http://tech.humlesite.eu/2017/01/13/using-regular-expression-to-merge-database-content-into-rich-text-format-template-documents/

PHP 示例如下所示:

<?php 

$file = file_get_contents('mergedoc.rtf');

// To temporary get rid of the escape characters...
$mergetext = str_replace("\\", "€€", $file); 

// New seven part regex with default value detection
$regex2 = '/<<((?:€€[a-z0-9]*|\}|\{|\s)*)([a-z0-9.\-\+_æøåÆØÅA-Z]*)((?:€€[a-z0-9]*|\}|\{|\s)*)([a-z0-9.\-\+_æøåÆØÅA-Z]*)((?:€€[a-z0-9]*|\}|\{|\s)*)(?:\s*:(.*?)\s*)?((?:€€[a-z0-9]*|\}|\{|\s)*)>>/';

// Find all the matches in it....
preg_match_all($regex2,$mergetext, $out, PREG_SET_ORDER);

// Lets see the result
var_dump($out); 

foreach ($out as $match) {
    $whole_tag = $match[0]; // The part we actually replace. 
    $start = $match[1]; // The start formatting that has been injected in our tag, if any
    $tag = $match[2]; // The tag word itself. 
    if (($match[4].$match[6]) != "") { //some sec-part tag or default value?
        $end = $match[5]; // The end formatting that might be inserted. 
        if ($end == "") {
            $end = $match[7]; // No end in 5, we try 7. 
        }
    } else {
        $end = $match[3]; // No second tag or default value, we find end in match-3 
    }

    $secPartTag = $match[4]; // Do we have inserted some formatting inside the tag word too ? 
    if ($secPartTag != "") {
        $tag .= $secPartTag; // Put it together with the tag word. 
    }
    $default_value = $match[6]; 

    // Simple selection of what we do with the tag. 
    switch ($tag) {
        case 'COMPANY_NAME': 
            $txt = "MY MERGE COMPANY EXAMPLE LTD"; 
            break; 
        case 'SOMEOTHERTAG':
            $txt = "SOME OTHER TEXT XX"; 
            break; 
        case 'THISHASDEFAULT':
            $txt = ""; 
            break; 

        default:
            $txt = "NOTAG"; 
    }
    if ($txt == "") {
        $txt = $default_value; 
    }
    // Create RTF Line breaks in text, if any. 
    $txt = str_replace(chr(10), chr(10)."\\line", $txt); 
    // Do the replace in the file. 
    $mergetext = str_replace($whole_tag, $start.$txt.$end, $mergetext); 
}
// Put back the escape characters. 
$file = str_replace("€€", "\\", $mergetext);
// Save to file. Extention .doc makes it open in Word by default. 
file_put_contents("ResultDoc.doc", $file); 

?>

【讨论】:

    猜你喜欢
    • 2019-07-18
    • 2020-07-04
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2013-05-26
    • 2019-07-30
    • 2019-10-16
    相关资源
    最近更新 更多