tags:

views:

95

answers:

4

description like <a href="myexample.com"></a> should return blank

+1  A: 

If you want to replace url's from dynamic content, you can do this with regular expressions, or an easier method like using phpQuery which will allow you to use a much method of looking up links within HTML, and replacing their HREF attribute.

phpQuery::newDocument("externalPage.html");
pq("a")->href("");

I've not used phpQuery for a while, but I believe that would do the trick. Also, if the links you are trying to remove are navigation, rss feeds, etc, you can use phpQuery to return only a particular portion of the externalPage, meaning you would no longer have to remove the links that are not within the portion you want.

For example, if you are trying to get an article from the external page that exists within a DIV having an ID of "articleBox", you could do this:

pq("div#articleBox");

That would return only that particular element, and the contents within it.

You may find that PHPSimpleHTMLDOMParser is easier to work with. Here's an example of how to use it against slashdot to scrape parts of the main page:

// Create DOM from URL
$html = file_get_html('http://slashdot.org/');

// Find all article blocks
foreach($html->find('div.article') as $article) {
    $item['title'] = $article->find('div.title', 0)->plaintext;
    $item['intro'] = $article->find('div.intro', 0)->plaintext;
    $item['details'] = $article->find('div.details', 0)->plaintext;
    $articles[] = $item;
}

print_r($articles);
Jonathan Sampson
A: 

sir i am fetching the data from other site and then there are links given like <a href="example.com">... </a> but i dont want that when i am showing the fetched data..means that the link is replaced with null

prateek
A: 

Use the php strip_tags function (http://us3.php.net/manual/en/function.strip-tags.php), it will remove all html tags from a string, so:

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);

Would output "Test paragraph. Other text", and the example you gave would return blank. Note that you can also specify some tags you want to allow, if there are some you still want to use.

Kazar
A: 

Are you trying to hide the links in the fetched data? If so, maybe you could you apply CSS to only that text (div or span) to hide all <a> tags> by setting display:none.

DOK