簡體   English   中英

如何使用php preg_replace正則表達式刪除href鏈接以外的所有內容

[英]How to remove all except the href link using php preg_replace regex

如何使用 preg_replace regex刪除href鏈接以外的所有內容。

<?
$string = "<li>123<a href=\"https://stackoverflow.com/questions/ask\"><img src=\"https://cdn.sstatic.net/Sites/stackoverflow/img/sprites.svg\"></a>remove me<a href=\"https://stackoverflow.com/questions/ask\"></a>Jumat, 20 April 2018 14:15 This string for removed.</li>"; 
echo preg_replace('%<a.*?</a>%i', '', $string);
?>

這是刪除所有href鏈接的代碼,但我希望不這樣做。 刪除所有但href鏈接。

例如:

輸入: <div>this outer <a href='#'>first link</a> is the best <span>destination</span></div><a href='#'>second link</a>

輸出: <a href='#'>first link</a><a href='#'>second link</a>

使用類似domdocument的解析器比使用正則表達式更好:

$html = "<div>this outer <a href='#'>first link</a> is the best <span>destination</span></div><a href='#'>second link</a>";
$doc = new DOMDocument();
$doc->loadHTML($html);
$links = $doc->getElementsByTagName('a');
foreach($links as $link) {
    echo $doc->saveHTML($link) . PHP_EOL;
}

演示: https//3v4l.org/rMUH1

似乎鍵盤有問題。 (請參閱此鏈接 )。 而是在這里嘗試。

另外,必須修改for循環,如下所示:

    <?php
    $string = "<li>1<a href=\"http://www.tribunnews.com/superskor/2018/04/20/link-live-streaming-psis-semarang-vs-persija-jakarta-di-indosiar\"><img src=\"http://cdn2.tstatic.net/tribunnews/foto/bank/thumbnails2/psis-semarang-vs-persija-jakarta_20180420_114602.jpg\" height=\"90\" width=\"120\" alt=\"psis-semarang-vs-persija-jakarta_20180420_114602.jpg\" class=\"pa5 shou \"></a><a href=\"http://www.tribunnews.com/superskor/2018/04/20/link-live-streaming-psis-semarang-vs-persija-jakarta-di-indosiar\" title=\"Link Live Streaming PSIS Semarang Vs Persija Jakarta di Indosiar\"></a>Jumat, 20 April 2018 14:15 WIB Kick-off laga PSIS versus Persija pukul 15.30 WIB dan disiarkan langsung oleh Indosiar.</li>"; 
    $string = preg_match_all('%<a.*?</a>%i', $string, $matches);
    for ($i = 0; $i < count($matches); $i++)
    {
        for ($j = 0; $j < count($matches[$i]); $j++)
        {
           echo $matches[$i][$j];
        } 
    }
    ?>

暫無
暫無

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

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