php code to extract all text links not image link

佐手、 提交于 2020-01-05 08:52:27

问题


I want to extract all text link from a webpage using simplehtmldom class. But i don't want image links.

<?
foreach($html->find('a[href]') as $element)
       echo $element->href . '<br>'; 
?>

above code shows all anchor links containing href attribute.

<a href="/contact">contact</a>
<a href="/about">about</a>
<a herf="/home"><img src="logo.png" /><a>

i want only /contact and /about not /home because it contains image instead of text


回答1:


<?php

foreach($html->find('a[href]') as $element)
{
    if (empty(trim($element->plaintext)))
        continue;

    echo $element->href . '<br>';
}



回答2:


<?
foreach($html->find('a[href]') as $element){
    if(!preg_match('%<img%', $element->href)){
        echo $element->href . '<br>';    
    }
}
?>



回答3:


It is possible to do that in css and with phpquery as:

$html->find('a:not(:has(img))')

This is not a feature that will likely ever come to simple though.



来源:https://stackoverflow.com/questions/15577708/php-code-to-extract-all-text-links-not-image-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!