6

说我有以下文字

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

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

非常感谢

4

8 回答 8

17

这将删除所有标签:

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

这将只删除<a>标签:

preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $string);
于 2009-09-01T22:44:52.437 回答
16

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

于 2009-09-01T22:45:29.143 回答
4
<?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 解析器

于 2009-09-01T22:58:52.003 回答
3

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

$data = str_replace('</a>', '', $data);
$data = preg_replace('/<a[^>]+href[^>]+>/', '', $data);
于 2009-09-01T22:44:07.180 回答
1

strip_tags()也可以使用。

在此处查看示例。

于 2012-07-09T07:11:30.927 回答
0

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

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);
    }
于 2010-11-01T16:24:52.477 回答
0
$pattern = '/href="([^"]*)"/';
于 2013-07-13T00:52:31.897 回答
-2

使用 str_replace

于 2009-09-01T22:42:14.283 回答