How to remove link with preg_replace();?

折月煮酒 提交于 2020-02-04 07:00:29

问题


I'm not sure how to explain this, so I'll show it on my code.

<a href="link.php">First</a> and 
<a href="link.php" class="delete">Second</a> and 
<a href="link.php">Third</a>

how can I delete opening <a href="link.php" class="delete"> and closing </a> but not the rest?

I'm asking for preg_replace(); and I'm not looking for DomDocument or others methods to do it. I just want to see example on preg_replace();

how is it achievable?


回答1:


Only pick the groups you want to preserve:

$pattern = '~(<a href="[^"]*" class="delete">)([^<]*)(</a>)~';
//                   1                           2      3
$result = preg_replace($pattern, '$2', $subject);

You find more examples on the preg_replace manual page.




回答2:


Since you asked me in the comments to show any method of doing this, here it is.

$html =<<<HTML
<a href="link.php">First</a> and 
<a href="link.php" class="delete">Second</a> and 
<a href="link.php">Third</a>
HTML;

$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$elems = $xpath->query("//a[@class='delete']");
foreach ($elems as $elem) {
    $elem->parentNode->removeChild($elem);
}

echo $dom->saveHTML();

Note that saveHTML() saves a complete document even if you only parsed a fragment.

As of PHP 5.3.6 you can add a $node parameter to specify the fragment it should return - something like $xpath->query("/*/body")[0] would work.




回答3:


$pattern = '/<a (.*?)href=[\"\'](.*?)\/\/(.*?)[\"\'](.*?)>(.*?)<\/a>/i';
$new_content = preg_replace($pattern, '$5', $content);



回答4:


$pattern = '/<a[^<>]*?class="delete"[^<>]*?>(.*?)<\/a>/';

$test = '<a href="link.php">First</a> and <a href="url2.html" class="delete">Second</a> and <a href="link.php">Third</a>';
echo preg_replace($pattern, '$1', $test)."\n";

$test = '<a href="link.php">First</a> and <a href="url2.html"><b class="delete">seriously</b></a> and <a href="link.php">Third</a>';
echo preg_replace($pattern, '$1', $test)."\n";

$test = '<a href="link.php">First</a> and <a href="url2.html" class="delete"><b class="delete">seriously</b></a> and <a href="link.php">Third</a>';
echo preg_replace($pattern, '$1', $test)."\n";

$test = '<a href="link.php">First</a> and <a  class="delete" href="url2.html">Second</a> and <a href="link.php">Third</a>';
echo preg_replace($pattern, '$1', $test)."\n";



回答5:


preg_replace('@<a href="[^"]+" class="delete">(.+?)</a>@', '$1', $html_string);

It is important to understand this is not an ideal solution. First, it requires markup in this exact format. Second, if there were, say, a nested anchor tag (albeit unlikely) this would fail. These are some of the many reasons why Regular Expressions should not be used for parsing/manipulating HTML.



来源:https://stackoverflow.com/questions/6778209/how-to-remove-link-with-preg-replace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!