php从文本中删除内容类型

时间:2012-02-11 08:33:45

标签: php regex

我有一些文字,例如$text="--e89a8f234aade3345704b8477b83 Content-Type: text/plain; charset=ISO-8859-1 this is a text. this is a text. --e89a8f234aade3345704b8477b83 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable this is a text.";

我希望输出为$output="this is a text. this is a text.";

还有一个例如: - $text="--14dae9340ba954ae0704b84acde9 Content-Type: text/plain; charset=ISO-8859-1 First Name: aaa Last Name: aaa --14dae9340ba954ae0704b84acde9 Content-Type: text/html; charset=ISO-8859-1 First Name: James Last Name: Cummings --14dae9340b";

输出应为$output="First Name: aaa Last Name: aaa";

我没有得到如何做到这一点。

2 个答案:

答案 0 :(得分:3)

如果您确定charset是ISO-8859-1且字符串不包含 - :

$a=explode("charset=ISO-8859-1",$text);
$b=explode("--",$a[1]);
$output=$b[0];

如果您仍然确定字符集,而不是 - :

$aa=substr($text,0,30);
$a=explode("charset=ISO-8859-1",$text);
$b=explode($aa,$a[1]);
$output=$b[0];

如果你甚至不确定字符集:

$aa=substr($text,0,30);
$a=explode("charset=",$text);
$a2=explode(" ",$a[1]);
$b=explode($aa,$a2[1]);
$output=$b[0];

答案 1 :(得分:1)

在字符串上使用preg_match,类似于

<?php
$text="--14dae9340ba954ae0704b84acde9 Content-Type: text/plain; charset=ISO-8859-1 First     Name: aaa Last\
 Name: aaa --14dae9340ba954ae0704b84acde9 Content-Type: text/html; charset=ISO-8859-1 First Name: James\
 Last Name: Cummings --14dae9340b";

$cnt = preg_match_all('/ISO-8859-1 (.+?) --/',$text,$array);
$array[1][0], "<br />",$array[1][1];

&GT;

第二个元素是$ array(位置1)包含与模式中的组匹配的$ text的所有子字符串。