Strip RTF strings with PHP - Regex [duplicate]

风格不统一 提交于 2019-12-12 17:05:09

问题


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.

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.

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+)"

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

Any regex gurus out there?


回答1:


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.



来源:https://stackoverflow.com/questions/17318041/strip-rtf-strings-with-php-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!