2011-10-27 58 views
0

这里是我的示例文本删除文本之前和之后文本,我想 - PHP

------=_Part_564200_22135560.1319730112436 
Content-Type: text/plain; charset=utf-8; name=text_0.txt 
Content-Transfer-Encoding: 7bit 
Content-ID: <314> 
Content-Location: text_0.txt 
Content-Disposition: inline 

I hate u 
------=_Part_564200_22135560.1319730112436 
Content-Type: image/gif; name=dottedline350.gif 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename=dottedline350.gif 
Content-ID: <dottedline350.gif> 

我需要能够提取“我恨U”。

我知道我可以使用爆炸做

$body_content = garbled crap above; 
$message_short = explode("------=_",$body_content); 
echo $message_short; 

的消息后,除去最后一部分,但我需要删除的第一部分,在最后一部分沿。

因此,上述爆炸功能做什么,我需要删除结束 现在我需要的东西,说

FIND“内容处置:内联”和之前 然后 找到任何删除沿途“--- --- = _'并且随后除去 然后 任何剩余部分= $ message_short

echo $ message_short;

看起来像

我恨ü

任何帮助,将不胜感激。


感谢@Marcus

这是代码,我现在和奇妙的作品。

$body_content2 = imap_qprint(imap_body($gminbox, $email_number)); 
$body_content2 = strip_tags ($body_content2);  
$tmobile_body = explode("------=_", $body_content2); 
$tmobile_body_short = explode("Content-Disposition: inline", $tmobile_body[1]); 
$tmobile_body_complete1 = trim($tmobile_body_short[1]); 
$tmobile_body_complete2 = explode("T-Mobile", $tmobile_body_complete1); 
$tmobile_body_complete3 = trim($tmobile_body_complete2[1]); 
+0

我也讨厌你。但是你可以使用[regular expression](http://regular-expressions.info)和['preg_match'](http://php.net/preg_match)来代替'explode'。但是,你有没有完整的blob?那么使用[PEAR mimeDecode](http://pear.php.net/package/Mail_mimeDecode)会更容易。 – mario

+0

你可以在双线上试试strstr。 – hafichuk

回答

0

如果你想这样做与爆炸,爆炸的“内容处置:内联”这里是你如何能做到:

$message_short = explode("------=_", $body_content); 
$message_shorter = explode("Content-Disposition: inline", $message_short[1]); 
$content = trim($message_shorter[1]); 
// $content == I hate you 

注意,这要求,最后一行是Content-Disposition: inline

如果要使用正则表达式来解决它,并使用双换行符作为分隔符这里是为你codesnippet:

$pattern = '/(?<=------=_Part_564200_22135560.1319730112436)(?:.*?\s\s\s)(.*?)(?=------=_Part_564200_22135560.1319730112436)/is'; 
preg_match_all($pattern, $body_content, $matches); 

echo($matches[1][0]); 

输出:

I hate you 

这里有一个链接codepad与示例文本。

+0

感谢Marcus,正是我在寻找的。 –

+0

谢谢,修改我的主要帖子,显示我使用的代码,完美工作。 –

相关问题