-1

How to remove all except the href link using preg_replace regex.

<?
$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);
?>

This is the code to remove all the href links but I want to be otherwise. Remove all but href links.

For example:

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

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

Toto
  • 89,455
  • 62
  • 89
  • 125
Hedi Herdiana
  • 101
  • 1
  • 11

2 Answers2

1

Using a parser like domdocument would be better than a regex:

$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;
}

Demo: https://3v4l.org/rMUH1

chris85
  • 23,846
  • 7
  • 34
  • 51
1

It seems there is an issue with codepad. (See this link). Instead try it here.

Also, the for loop has to be modified as follows:

    <?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];
        } 
    }
    ?>
Matt.G
  • 3,586
  • 2
  • 10
  • 23