How to remove all except the href link using php 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>