PHP-在RTF/txt文件中查找/替换文本

PHP-在RTF/txt文件中查找/替换文本,php,str-replace,Php,Str Replace,我遇到了一个问题,就是如何找到特定的文本并将其替换为其他文本。 我只使用.rtf和.txt文件测试下面的代码。我还确保文件在我的服务器中是可写的 这是一种碰运气的情况,我很好奇我的代码是不是错了,或者这只是打开和操作文件的怪异之处 <?php $filelocation = '/tmp/demo.txt'; $firstname = 'John'; $lastname = 'Smith'; $output = file_get_contents($filelocation); $out

我遇到了一个问题,就是如何找到特定的文本并将其替换为其他文本。 我只使用
.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的内容,很难说清楚。我的第一个猜测是,使用括号作为指针可能会有问题。我会尝试改变一些RTF没有使用的东西,比如百分号或星号。例如:%%FIRSTNAME%%,**FIRSTNAME**(这当然是假设您可以控制demo.txt的内容。)

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

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

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

?>

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

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

?>


你能在demo.txt中放一些文本吗?这样我们就可以知道什么是错误的。好的,我来试试……奇怪-用一个简单的文本文件尝试了你的代码,它完全按照预期工作。不知道你的东西怎么了。上传演示文件?让我看看我是否明白了。你想把
[[FIRSTNAME]]
替换为
John
,把
[[FIRSTNAME]]]
替换为
Smith
?确切地说,这是一个查找并替换的场景。你能把一些文本放在demo.txt中吗,这样我们就可以知道什么是错误的好点,我会试试的…奇怪-用一个简单的文本文件尝试了你的代码,它完全按照预期工作。不知道你的东西怎么了。上传演示文件?让我看看我是否明白了。你想用
John
替换
[[FIRSTNAME]]]
,用
Smith
替换
[[FIRSTNAME]]]
?确切地说,这是一个查找和替换方案。看起来这确实是一个
.rtf
问题。我已经用
.txt
对它进行了测试,它在那里工作得很好。也许有一些奇怪的不可见的格式添加剂是由Microsoft Word产生的。是的,RTF添加了所有类型的格式字符。如果我的回答有帮助的话,请投赞成票或做标记。谢谢你说得对。我需要投赞成票。我用你的想法尝试了不同的前/后标记参数。另外,我删除了
.rtf
中的所有格式,似乎这是一个解决方案。富文本万岁,有点。我已经遇到过好几次了。很高兴我能帮忙;)看来这确实是一个
.rtf
问题。我已经用
.txt
对它进行了测试,它在那里工作得很好。也许有一些奇怪的不可见的格式添加剂是由Microsoft Word产生的。是的,RTF添加了所有类型的格式字符。如果我的回答有帮助的话,请投赞成票或做标记。谢谢你说得对。我需要投赞成票。我用你的想法尝试了不同的前/后标记参数。另外,我删除了
.rtf
中的所有格式,似乎这是一个解决方案。富文本万岁,有点。我已经遇到过好几次了。很高兴我能帮忙;)