2013-08-27 66 views
0

我得到了一個包含html代碼的* .txt文件。我想刪除:preg_replace - 如何刪除<textarea cols =「100」(...)>和</textarea>?

<textarea cols="100" rows="50" name="newcontent" id="newcontent"> 

</textarea> 

我能夠除去第一標籤與此:

$content_replaced = preg_replace ("/<textarea cols=\"100\" rows=\"50\" name=\"newcontent\" id=\"newcontent\">/", "", $replace); 

,但無法弄清楚如何更換</textarea的>以及。 謝謝:)

+0

所以,你可以用一無所有,但不能代替'b'沒有?因爲在那個位置上,我會首先考慮用'b'來做和我一樣的'a'。 – Jon

+1

您不使用HTML上的正則表達式。使用DOM,移除標籤/元素變得微不足道。 –

+1

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 –

回答

0

嘗試:基本上,我只是說

$content_replaced = preg_replace ("/<textarea cols=\"100\" rows=\"50\" name=\"newcontent\" id=\"newcontent\">.*?<\/textarea>/", "", $replace); 

*?到你的正則表達式。它將會做的是「非貪婪地」將所有內容匹配到下一個結束標記。

+0

不會在標籤之間移除內容嗎? – Dexa

+0

是的,我現在假設你只想刪除結束標記。爲什麼不只是做第二個preg_replace,並簡單地匹配「」並將其替換爲「」? – user2588667

0

你可以試試這個正則表達式。這裏是例子。

$string = "sdfsd <textarea cols=\"100\" rows=\"50\" name=\"newcontent\" id=\"newcontent\"> some text here </textarea>"; 
$result = preg_replace("/.*?>(.*)<.*?>$/","$1",$string); 
echo $result; 

只有當您只有一個標籤符合時纔可以使用。

相關問題