如何使用php preg_replace regex删除除href链接之外的所有内容

时间:2018-04-20 13:58:07

标签: php regex preg-replace

如何使用 preg_replace 正则表达式删除除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>

2 个答案:

答案 0 :(得分:1)

使用像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

答案 1 :(得分:1)

似乎键盘有问题。 (见link)。而是试试here

此外,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];
        } 
    }
    ?>