PHP正则表达式删除HTML文档中的标签

发布于 2024-08-03 19:06:04 字数 235 浏览 2 评论 0原文

假设我有以下文本,

..(content).............
<A HREF="http://foo.com/content" >blah blah blah </A>
...(continue content)...

我想删除链接,并且我想删除标签(同时保留中间的文本)。我如何使用正则表达式执行此操作(因为 URL 都会不同)

非常感谢

Say I have the following text

..(content).............
<A HREF="http://foo.com/content" >blah blah blah </A>
...(continue content)...

I want to delete the link and I want to delete the tag (while keeping the text in between). How do I do this with a regular expression (since the URLs will all be different)

Much thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

时光沙漏 2024-08-10 19:06:04

这将删除所有标签:

preg_replace("/<.*?>/", "", $string);

这将仅删除 标签:

preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $string);

This will remove all tags:

preg_replace("/<.*?>/", "", $string);

This will remove just the <a> tags:

preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $string);
-小熊_ 2024-08-10 19:06:04

尽可能避免使用正则表达式,尤其是在处理 xml 时< /a>.在这种情况下,您可以使用 strip_tags()simplexml,具体取决于您的字符串。

Avoid regular expressions whenever you can, especially when processing xml. In this case you can use strip_tags() or simplexml, depending on your string.

度的依靠╰つ 2024-08-10 19:06:04
<?php
//example to extract the innerText from all anchors in a string
include('simple_html_dom.php');

$html = str_get_html('<A HREF="http://foo.com/content" >blah blah blah </A><A HREF="http://foo.com/content" >blah blah blah </A>');

//print the text of each anchor    
foreach($html->find('a') as $e) {
    echo $e->innerText;
}
?>

请参阅 PHP 简单 DOM 解析器

<?php
//example to extract the innerText from all anchors in a string
include('simple_html_dom.php');

$html = str_get_html('<A HREF="http://foo.com/content" >blah blah blah </A><A HREF="http://foo.com/content" >blah blah blah </A>');

//print the text of each anchor    
foreach($html->find('a') as $e) {
    echo $e->innerText;
}
?>

See PHP Simple DOM Parser.

罪歌 2024-08-10 19:06:04

不漂亮,但可以完成工作:

$data = str_replace('</a>', '', $data);
$data = preg_replace('/<a[^>]+href[^>]+>/', '', $data);

Not pretty but does the job:

$data = str_replace('</a>', '', $data);
$data = preg_replace('/<a[^>]+href[^>]+>/', '', $data);
白云不回头 2024-08-10 19:06:04

也可以使用strip_tags()

请参阅此处的示例。

strip_tags() can also be used.

Please see examples here.

¢好甜 2024-08-10 19:06:04
$pattern = '/href="([^"]*)"/';
$pattern = '/href="([^"]*)"/';
无敌元气妹 2024-08-10 19:06:04

我用它来用文本字符串替换锚点......

function replaceAnchorsWithText($data) {
        $regex  = '/(<a\s*'; // Start of anchor tag
        $regex .= '(.*?)\s*'; // Any attributes or spaces that may or may not exist
        $regex .= 'href=[\'"]+?\s*(?P<link>\S+)\s*[\'"]+?'; // Grab the link
        $regex .= '\s*(.*?)\s*>\s*'; // Any attributes or spaces that may or may not exist before closing tag
        $regex .= '(?P<name>\S+)'; // Grab the name
        $regex .= '\s*<\/a>)/i'; // Any number of spaces between the closing anchor tag (case insensitive)

        if (is_array($data)) {
            // This is what will replace the link (modify to you liking)
            $data = "{$data['name']}({$data['link']})";
        }
        return preg_replace_callback($regex, array('self', 'replaceAnchorsWithText'), $data);
    }

I use this to replace the anchors with a text string...

function replaceAnchorsWithText($data) {
        $regex  = '/(<a\s*'; // Start of anchor tag
        $regex .= '(.*?)\s*'; // Any attributes or spaces that may or may not exist
        $regex .= 'href=[\'"]+?\s*(?P<link>\S+)\s*[\'"]+?'; // Grab the link
        $regex .= '\s*(.*?)\s*>\s*'; // Any attributes or spaces that may or may not exist before closing tag
        $regex .= '(?P<name>\S+)'; // Grab the name
        $regex .= '\s*<\/a>)/i'; // Any number of spaces between the closing anchor tag (case insensitive)

        if (is_array($data)) {
            // This is what will replace the link (modify to you liking)
            $data = "{$data['name']}({$data['link']})";
        }
        return preg_replace_callback($regex, array('self', 'replaceAnchorsWithText'), $data);
    }
以歌曲疗慰 2024-08-10 19:06:04

使用str_replace

use str_replace

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文