使用 PHP 去除 RTF 字符串 - 正则表达式 [英] Strip RTF strings with PHP - Regex

查看:58
本文介绍了使用 PHP 去除 RTF 字符串 - 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的数据库中的一列包含 RTF 字符串,我想使用 PHP 去除这些字符串,只留下中间的句子.

A column in the database I work with contains RTF strings, I would like to strip these out using PHP, leaving just the sentence between.

如果我没记错的话,它是一个 MS SQL 数据库 2005.

It is a MS SQL database 2005 if I recall correctly.

从数据库中提取的字符串类型的示例(需要更多让我知道,其余的都相似):

An example of the kind of strings pulled from the database (need any more let me know, all the rest are similar):

{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.\lang2057\fs17\par 
}

我希望将其剥离以仅返回:

I would like this to be stripped to only return:

ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.

现在,我已经成功地为以前的项目在 ASP.NET 中剥离了字符,但是我想使用 PHP 来这样做.这是我在 ASP.NET 中使用的正则表达式,我可以添加它:

Now, I have successfully managed to strip the characters in ASP.NET for a previous project, however I would like to do so using PHP. Here is the regular expression I used in ASP.NET, which works flawlessly may I add:

"(\{.*\})|}|(\\\S+)"

但是,当我尝试在 PHP 中使用带有 preg_replace 的相同表达式时,它不会去除一半的字符.

However when I try to use the same expression in PHP with a preg_replace it does not strip half of the characters.

有正则表达式专家吗?

推荐答案

使用此代码.它会正常工作.

Use this code. it will work fine.

$string = preg_replace("/(\{.*\})|}|(\\\S+)/", "", $string);

请注意,我在正则表达式的开头和结尾都添加了/".

Note that I added a '/' in the beginning and at the end '/' in the regex.

这篇关于使用 PHP 去除 RTF 字符串 - 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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