2011-08-30 63 views
0

我正在解析一個XML文件,它的創建者卡在一羣完全沒用的社交媒體信息中。我想在將數據插入數據庫之前刪除它。strpos刪除文本/ html

問題是,它不是都一樣,有些事件是:

Be a Social Butterfly! Connect & Learn More Below: Website • Facebook • Yelp

一些有更多的社交網站上市,有些還少。我真的很想刪除整個部分。在運行strip_tags之後,這也是一個vardump。原來這個樣子的:

<strong>Be a Social Butterfly! Connect & Learn More Below:</br></strong> 
<a target="_blank" href="http://www.kiran-indian.com">Website</a> •<a target="_blank" href="http://www.facebook.com/pages/Kiran-Indian-Cuisine/55785994435"> Facebook</a> • <a target="_blank" href="http://www.yelp.com/biz/kiran-indian-cuisine-new-york">Yelp</a> 

我使用的preg_replace擺脫日整個句子的「社交人脈......」與

$description = strip_tags(preg_replace('/\bBe a Social Butterfly! Connect & Learn More Below\b/', '', $value['redemptionLocations']['description'])); 

我的一個朋友建議使用strpos的找到第一個/最後一個部分和substr以刪除它們之間的所有內容,但遺憾的是,我還沒有進一步弄清楚如何做到這一點。

在此先感謝!

描述字段:

  
Food always does one thing. It helps keep you alive. But it can do more. It can be an experience that educates, transports, and invigorates you. Lunch or dinner at <a target="_blank" href="http://www.kiran-indian.com/home.htmls">Kiran Indian Cuisine</a> a lot more than a chance to keep from starving for another day --- it’s a chance to depart from the norm with delicious homemade dishes using the freshest of ingredients and the most aromatic seasoning available. They are open 7 days a week from 11 a.m. to 11 p.m. and accept all the major credit cards, plus when you order online from the surrounding area, delivery is 100% free of charge.</br></br> 

<strong>Be a Social Butterfly! Connect & Learn More Below:</br></strong> 
<a target="_blank" href="http://www.kiran-indian.com">Website</a> •<a target="_blank" href="http://www.facebook.com/pages/Kiran-Indian-Cuisine/55785994435"> Facebook</a> • <a target="_blank" href="http://www.yelp.com/biz/kiran-indian-cuisine-new-york">Yelp</a> 

似乎粘貼代碼到這裏自動調整ASCI /等。

+0

要刪除的那句話後列出的網站「..Butterfly ..」,無論網站的數量? – Samih3

+0

是的,從該句子中刪除所有內容,直到結束。我只是不知道這是如何工作的,因爲它技術上解析 Mike

+0

裏面的所有信息然後格式是這樣的:是一個社會蝴蝶!..網站列表,ryt? – Samih3

回答

0

您需要在整個文本中找到第一個字符串的位置,請使用strpos,然後您需要在要刪除的塊的末尾找到位置,然後再使用strpos。現在,您有要刪除的塊的開始和結束點,請使用substr_replace將其替換爲''substr_replace將塊的長度作爲第四個參數移除,而不是第三個參數的位置,所以您需要從第二個位置int中減去第一個位置int來計算出長度。

$feedtext='<description> this part is important... be a social butterfly .. blah blah etc etc whatever whatever </description>'; 

$pos1=strpos($feedtext,'be a social butterfly'); 
$pos2=strpos($feedtext,'</description>'); 
$len=$pos2-$pos1; 
$newtext=substr_replace($feedtext,'',$pos1,$len); 

echo $newtext; 

測試:http://www.ideone.com/1X5gI

+0

的最後部分我試過上面的代碼,我用$ feedtext = $ value ['redemptionLocations'] ['description']取代了$ feedtext,它輸出的所有內容 – Mike

+0

@mike你'可能需要改變pos1和pos2變量中尋找的東西(它們區分大小寫)。代碼工作,我鏈接到一個工作示例。如果它不起作用,那是因爲你想要獲得位置的字符串不在你傳遞給它的草垛中。 – profitphp

+0

在asci字符上做這個工作嗎?我試圖設置pos1爲'<強>是一個社交蝴蝶'和pos2爲'< /一>' – Mike