2011-10-27 59 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

謝謝,修改我的主要帖子,顯示我使用的代碼,完美工作。 –

相關問題