开发者

How to remove a link from content using php?

$text = file_get_contents('http://www.example.com/file.php?id=name');
echo preg_replace('#<a.*?>.*?</a>#i', '', $text)

th开发者_运维知识库e link contains this content:

text text text. <br><a href='http://www.example.com' target='_blank' title='title' style='text-decoration:none;'>name</a>

what is the problem at this script?


You can't parse HTML with regular expressions. Use an XML/HTML parser.


Tempted to flag your question, but there's no option for "Report user for summoning Cthulhu"

I'd recommend reading: http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html

RegEx is very poor and not at all intended to parse HTML. That's why there are HTML parsing libraries. Find and use one for PHP. :)


use <a[^>]+>[^<]*</a> (works fine as long as theres just text and no tags inside the a element)


USE strip_tags this way

$t = 'http://yoururl.com/test1.php';
$t1 = file_get_contents($t);
$text = strip_tags($t1);

it should work getting rid of all the links inside the page you are reading, visit the reference anyway, it may not work for complicated elements http://php.net/manual/en/function.strip-tags.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜