开发者

How to remove link with preg_replace();?

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_repla开发者_StackOverflow中文版ce(); 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?


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.


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.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜