views:

2179

answers:

6

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

A: 

use str_replace

nandocurty
how should he do this with different href strings ?
Rufinus
(I'm not the downvoter, but as it seems he will not explain why he downvoted, which is not that helpful, might I add, let's guess why...) With str_replace, you cannot specify a "pattern", which is a problem, as the URL can change ; and even if it did not change, you'd have to use two calls to str_replace : one for the openig tag, and one for the closing one, as you want to keep what is beetween.
Pascal MARTIN
+1  A: 

Not pretty but does the job:

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

This will remove all tags:

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

This will remove just the <a> tags:

preg_replace("/<\\/?a(\\s+.*?>|>)/", "", $string);
nickf
won't that wipe out every tag?
Nerdling
isn't that what was asked for?
nickf
+8  A: 

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

soulmerge
+4  A: 
<?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.

karim79