簡體   English   中英

Preg_replace刪除所有iframe,最后一個除外

[英]Preg_replace delete all iframes, except the last one

我試圖刪除所有iframe,但保留最后一個iframe。

這是我加載的html

<br />CAP 01:<br /><iframe src="mysitevideo.com/embed-291dz33facsl-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />CAP 02: <br /><iframe src="http://mysitevideo.com/embed-3bp1mdixoiyc-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />CAP 03: <br /><iframe src="http://mysitevideo.com/embed-0d213t7vp8f2-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />

輸出應為: <iframe src="http://mysitevideo.com/embed-0d213t7vp8f2-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe>

這是我的職責

function DELiframeEXCEPTlast($str){
$str = preg_replace('#<iframe src="(.*?)" width="540" height="320" frameborder="0" scrolling="no"></iframe>#','',$str);
return $str;
}

我的代碼刪除了所有的事件。 有什么想法可以保留最后一個?

也許是這樣的嗎?

function DELiframeEXCEPTlast($str){
  $pattern='#<iframe src="(.*?)" width="540" height="320" frameborder="0" scrolling="no"></iframe>#';
  return preg_replace($pattern,'',$str,preg_match_all($pattern,$str)-1);
}

使用preg_split並返回返回數組中的最后一項。

http://www.php.net/manual/zh/function.preg-split.php

盡管為此使用正則表達式不是最好的主意。 您確實應該考慮使用php中的DOM對象來執行此操作。

http://www.php.net/manual/zh/book.dom.php

您可以在regexp中設置另一個iframe,如下所示:

function DELiframeEXCEPTlast($str){
$str = preg_replace('#(<iframe[^>]*></iframe>)*.*(<iframe[^>]*></iframe>)#','$2',$str);
return $str;
}
var_dump(DELiframeEXCEPTlast('<br />CAP 01:<br /><iframe src="mysitevideo.com/embed-291dz33facsl-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />CAP 02: <br /><iframe src="http://mysitevideo.com/embed-3bp1mdixoiyc-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />CAP 03: <br /><iframe src="http://mysitevideo.com/embed-0d213t7vp8f2-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />'));

這將輸出

string(104) "<iframe src="http://mysitevideo.com/embed-0d213t7vp8f2-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />"

您的正則表達式要替換所有iframe的原因是因為它與所有iframe匹配。 嘗試使用preg_matchpreg_match_all找出將發現的內容(這是將被替換的列表)

如果您真的想通過正則表達式(通常被認為是“ 一件壞事 ”)來執行此操作,則向每個<iframe...></iframe>結構中添加一個id標記,然后將其添加<iframe...></iframe>則表達式中,然后查看那會得到你的。

這里有很多答案,建議使用PHP中的DOM函數來更改HTML代碼而不是正則表達式。 我還沒有親自使用過它們(以前從來不需要通過PHP調整HTML),但這可能是一條更好的選擇。

由於您正在解析和修改HTML,因此我強烈建議使用DOM Parser 考慮以下代碼:

$html = '<br />CAP 01:<br /><iframe src="mysitevideo.com/embed-291dz33facsl-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />CAP 02: <br /><iframe src="http://mysitevideo.com/embed-3bp1mdixoiyc-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />CAP 03: <br /><iframe src="http://mysitevideo.com/embed-0d213t7vp8f2-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br />';

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$xpath = new DOMXPath($doc);
$nlist = $xpath->query("//iframe");

$numnodes = $nlist->length;
// run loop till $numnodes-1
for($i=0; $i < $numnodes-1; $i++) {
   $node = $nlist->item($i);
   // delete it
   $node->parentNode->removeChild($node);
}

$newHTML =  $doc->saveHTML();
echo $newHTML;

輸出:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><br>CAP 01:<br><br>CAP 02: <br><br>CAP 03: <br>
<iframe src="http://mysitevideo.com/embed-0d213t7vp8f2-540x320.html" width="540" height="320" frameborder="0" scrolling="no"></iframe><br></body></html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM