删除我想要的文本之前和之后的文本 - PHP

时间:2011-10-27 17:38:48

标签: 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>

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

我知道我可以使用explode来删除消息之后的最后一部分

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

但我需要删除第一部分以及最后一部分。

所以,上面的爆炸函数做了我需要删除的结果 现在我需要一些说法

查找'内容 - 处置:内联'并删除之前的任何内容 然后 找到'------ = _'并删除之后的任何内容 然后 剩余的东西= $ 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]);

1 个答案:

答案 0 :(得分:0)

如果你想爆炸并爆炸“Content-Disposition:inline”,你可以这样做:

$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

如果您想使用正则表达式解决它并使用双线换行作为分隔符,这里是您的代码段:

$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带示例文字的链接。