php - Remove href links and label using html dom parser -
first getting html of web page , removing href links appear on left or right side of page (not in page body). href links being removed labels not being removed.
example:
<a href='http://test.blogspot.com/2012/11/myblog.html'>london</a>
links being removed not it's label i.e. 'london'. how can remove complete row in html source? using following code it:
$string = strip_tags($html_source_code, '<a>', true); function strip_tags($text, $tags = '', $invert = false) { preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); $tags = array_unique($tags[1]); if(is_array($tags) , count($tags) > 0) { if($invert == false) { return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); } else { return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); } } elseif($invert == false) { return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); } return $text; }
if use code, fatal error: cannot redeclare strip_tags().
changing name function my_strip_tags works fine.
function my_strip_tags($text, $tags = '', $invert = false) { preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); $tags = array_unique($tags[1]); if(is_array($tags) , count($tags) > 0) { if($invert == false) { return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); } else { return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); } } elseif($invert == false) { return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); } return $text; } $html_source_code = "beginning of content ... <a href='http://test.blogspot.com/2012/11/myblog.html'>london</a> ... end of content."; echo "<p>".$html_source_code."</p>"; $string = my_strip_tags($html_source_code, '<a>', true); echo "<p>".$string."</p>";
that prints:
beginning of content ... london... end of content.
beginning of content ... ... end of content.
Comments
Post a Comment