在 PHP (REGEX) 中连接 RTF 文件 [英] Concatenate RTF files in PHP (REGEX)

查看:38
本文介绍了在 PHP (REGEX) 中连接 RTF 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,它接受用户上传的 RTF 文档并将一些个人数据合并到信件中(姓名、地址等),并为多人执行此操作.我合并信件内容,然后将其与下一个合并信件内容合并,用于所有人记录.

I've got a script that takes a user uploaded RTF document and merges in some person data into the letter (name, address, etc), and does this for multiple people. I merge the letter contents, then combine that with the next merge letter contents, for all people records.

实际上,我将单个 RTF 文档合并到自身中,以便我需要将信件合并到多个人的记录中.但是,我需要首先删除关闭的 RTF 标记并打开每个合并的 RTF 标记,否则 RTF 将无法正确呈现.这听起来像是正则表达式的工作.

Affectively I'm combining a single RTF document into itself for as many people records to which I need to merge the letter. However, I need to first remove the closing RTF markup and opening of the RTF markup of each merge or else the RTF won't render correctly. This sounds like a job for regular expressions.

本质上我需要一个可以删除整个字符串的正则表达式:

Essentially I need a regex that will remove the entire string:

}\n\page 任何东西 \par

}\n\page ANYTHING \par

例如,此正则表达式将匹配:

Example, this regex would match this:

crap
}
\page{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\f0\fs20 September 30, 2008\par
more crap

所以我可以做到:

crap
\page
more crap

RegEx 是最好的方法吗?

Is RegEx the best approach here?

更新:为什么我必须使用 RTF?

UPDATE: Why do I have to use RTF?

我想让用户上传一个格式信函,然后系统将使用它来创建合并的信函.由于 RTF 是纯文本,我可以很容易地在代码中做到这一点.我知道,RTF 是规范的灾难,但我不知道其他任何好的选择.

I want to enable the user to upload a form letter that the system will then use to create the merged letters. Since RTF is plain text, I can do this pretty easily in code. I know, RTF is a disaster of a spec, but I don't know any other good alternative.

推荐答案

在这种情况下,我会质疑 RTF 的使用.我不太清楚您总体上想要做什么,所以我不一定能提出更好的建议,但如果您可以尝试更广泛地解释您的项目,也许我可以提供帮助.

I would question the use of RTF in this case. It's not entirely clear to me what you're trying to do overall, so I can't necessarily suggest anything better, but if you can try to explain your project more broadly, maybe I can help.

如果这确实是您想要的方式,那么根据您的输入,此正则表达式为我提供了正确的输出:

If this is really the way you want to go though, this regex gave me the correct output given your input:

$output = preg_replace("/}\s?\n\\\\page.*?\\\\par\s?\n/ms", "\\page\n", $input);

这篇关于在 PHP (REGEX) 中连接 RTF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆