PHP regular expression to remove tags in HTML docu

2019-01-24 14:19发布

问题:

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

回答1:

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



回答2:

This will remove all tags:

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

This will remove just the <a> tags:

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


回答3:

<?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.



回答4:

Not pretty but does the job:

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


回答5:

strip_tags() can also be used.

Please see examples here.



回答6:

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);
    }


回答7:

$pattern = '/href="([^"]*)"/';


回答8:

use str_replace