我有一个ERP系统,它以RTF格式存储文本,我试图从中提取纯文本。
我搜索了谷歌并找到了像this one和其他一些使用REGEX替换的解决方案,但它们似乎都没有用。我总是得到NULL
或}}}
或完全错误的东西。
这是我试过的REGEX:
$matches = array('/\{\\\\(.+?)\}/','/\\\\(.+?)\b/');
$row['text'] = preg_replace($matches,'',$row['text']);
然而它返回:}}}
这是我的RTF数据:
{\rtf1\deff0{\fonttbl{\f0 Calibri;}{\f1 Arial;}}{\colortbl ;\red0\green0\blue255 ;}{\*\defchp \fs22}{\*\listoverridetable}{\stylesheet {\ql\fs22 Normal;}{\*\cs1\f1\fs20 Default Paragraph Font;}{\*\cs2\sbasedon1\f1\fs20 Line Number;}{\*\cs3\ul\fs22\cf1 Hyperlink;}{\*\ts4\tsrowd\fs22\ql\trautofit1\tscellpaddfl3\tscellpaddl108\tscellpaddfr3\tscellpaddr108\tsvertalt\cltxlrtb Normal Table;}{\*\ts5\tsrowd\sbasedon4\fs22\ql\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trbrdrh\brdrs\brdrw10\trbrdrv\brdrs\brdrw10\trautofit1\tscellpaddfl3\tscellpaddl108\tscellpaddfr3\tscellpaddr108\tsvertalt\cltxlrtb Table Simple 1;}}\nouicompat\splytwnine\htmautsp\sectd\pard\plain\ql{\f1\fs20\cf0 Migration Fileserverdaten innerhalb derselben oder einer vertrauten Dom\u228\'e4ne}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 Anpassung der Laufwerksfreigaben}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 Freigabenerstellung wie Bestand (weitere Absprachen hierzu m\u246\'f6glich)}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 Hostname(n) Quellsystem(e):}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 Hostname Zielsystem:}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 Bekanntes Datenvolumen:}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 Clientseitige Nacharbeiten aufgrund fest vergebener Einstellungen}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 erfolgen nach Aufwand oder durch den Auftraggeber.}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 Es wird im Besonderen darauf hingewiesen, da\u223\'df Datei- und Ornderberechtigungen 1:1 beibehalten werden. Die Neuvergabe jedweder Datei- oder Ordnerberechtigung kann nach Aufwand und Anweisung des Auftraggebers durchgef\u252\'fchrt werden.}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 Voraussetzungen zur Zusatzaufwandsfreien Durchf\u252\'fchrung:}\f1\fs20\par\pard\plain\ql{\f1\fs20\cf0 Gigabit Switching zwischen allen Quell- und Zielsystemen, Vollzugriff auf den zu migrierenden Datenbestand}\f1\fs20\par\pard\plain\ql\f1\fs20\par}
编辑2019:对于所有发现这个问题的人我使用这个单一项目,因为4年没有问题https://github.com/henck/rtf-html-php/blob/master/rtf-html-php.php
经过一些脑力,我得到了一个解决方案:
试试这个正则表达式:
"{\*?\\.+(;})|\s?\\[A-Za-z0-9]+|\s?{\s?\\[A-Za-z0-9]+\s?|\s?}\s?"
这意味着用。替换你的代码
$count = null;
$matches = array('"{\*?\\.+(;})|\s?\\[A-Za-z0-9]+|\s?{\s?\\[A-Za-z0-9]+\s?|\s?}\s?"');
$row['text'] = preg_replace($matches,'',$row['text'], -1, $count);
你可以在这里找到一个Rtf文本提取器:
以下是如何使用它的示例:
include ( 'path/to/RtfTexter.phpclass' ) ;
$doc = new RtfTexter ( 'sample.rtf' ) ;
echo $doc -> AsString ( ) ; // Echo text contents to stdout
$doc -> SaveTo ( 'sample.txt' ) ; // Save text contents to file 'sample.txt'
我正在发布这个问题的一般解决方案,其他人跳到这里。
public static function converToPlain($text){
$text = preg_replace('"{\*?\\\\.+(;})|\\s?\\\[A-Za-z0-9]+|\\s?{\\s?\\\[A-Za-z0-9‹]+\\s?|\\s?}\\s?"', '', $text);
return $text;
}
大家好我正在编写此代码读取rtf文件纯文本此代码工作100%
那里的PHP代码:
$text = file_get_contents('testfile.rtf');
if (!strlen($text)) {
echo "bad file";
exit();
}
// we'll try to fix up the parts of the rtf as best we can
// clean up the file a little to simplify parsing
$text=str_replace("\r",' ',$text); // returns
$text=str_replace("\n",' ',$text); // new lines
$text=str_replace(' ',' ',$text); // double spaces
$text=str_replace(' ',' ',$text); // double spaces
$text=str_replace(' ',' ',$text); // double spaces
$text=str_replace(' ',' ',$text); // double spaces
$text=str_replace('} {','}{',$text); // embedded spaces
// skip over the heading stuff
$j=strpos($text,'{',1); // skip ahead to the first part of the header
$loc=1;
$t="";
$ansa="";
$len=strlen($text);
getpgraph(); // skip by the first paragrap
while($j<$len) {
$c=substr($text,$j,1);
if ($c=="\\") {
// have a tag
$tag=gettag();
if (strlen($tag)>0) {
// process known tags
switch ($tag) {
case 'par':
$ansa.="\r\n";
break;
// ad a list of common tags
// parameter tags
case 'spriority1':
case 'fprq2':
case 'author':
case 'operator':
case 'sqformat':
case 'company':
case 'xmlns1':
case 'wgrffmtfilter':
case 'pnhang':
case 'themedata':
case 'colorschememapping':
$tt=gettag();
break;
case '*':
case 'info':
case 'stylesheet':
// gets to end of paragraph
$j--;
getpgraph();
default:
// ignore the tag
}
}
} else {
$ansa.=$c;
}
$j++;
}
$ansa=str_replace('{','',$ansa);
$ansa=str_replace('}','',$ansa);
echo "<pre>$ansa</pre>";
function getpgraph() {
// if the first char after a tag is { then throw out the entire paragraph
// this has to be nested
global $text;
global $j;
global $len;
$nest=0;
while(true) {
$j++;
if ($j>=$len) break;
if (substr($text,$j,1)=='}') {
if ($nest==0) return;
$nest--;
}
if (substr($text,$j,1)=='{') {
$nest++;
}
}
return;
}
function gettag() {
// gets the text following the / character or gets the param if it there
global $text;
global $j;
global $len;
$tag='';
while(true) {
$j++;
if ($j>=$len) break;
$c=substr($text,$j,1);
if ($c==' ') break;
if ($c==';') break;
if ($c=='}') break;
if ($c=="\\") {
$j--;
break;
}
if ($c=="{") {
//getpgraph();
break;
}
if ((($c>='0')&&($c<='9'))||(($c>='a')&&($c<='z'))||(($c>='A')&&($c<='Z'))||$c=="'"||$c=="-"||$c=="*" ){
$tag=$tag.$c;
} else {
// end of tag
$j--;
break;
}
}
return $tag;
}