dongmieqiao3152 2014-11-30 04:43
浏览 65
已采纳

使用php解析html页面以找出分配了链接的文本

say i have html code like this

$html = "This is some stuff right here. <a href='index.html'>Check this out!</a> <a href=herp.html>And this is another thing!</a> <a href=\"derp.html\">OH MY GOSH</a>";

i am trying to get values of href and also on which anchor work i mean check this out text i am able to get href value by following this code

$displaybody->find('a ') as $element;
echo $element;

well it works for me but how do i get value of check this out could you guys help me out. i did search but i am not able to find it out . thanks in advance

my actual html look like this

<a href="www.myurl/point.html" class="l" style="color:#436DBA;" onclick="return rs(this,'8 Stunning Linguistic Miracles of The Holy Quran | Kinetic Typography 144p (Video Only).mp4');">&raquo; Download MP4 &laquo;</a> - <b>144p (Video Only)</b> - <span> 19.1</span> MB<br />

my href look like this above code return download mp4 and i want it like downloadmp4 114p (video only) 19.1 mb how do i do that

  • 写回答

1条回答 默认 最新

  • doumu1873 2014-11-30 04:48
    关注

    If what you are using now is the SimpleHTMLDOM, then ->innertext works fine on that anchor elements that you have found:

    include 'simple_html_dom.php';
    $html = "This is some stuff right here. <a href='index.html'>Check this out!</a> <a href=herp.html>And this is another thing!</a> <a href=\"derp.html\">OH MY GOSH</a>";
    
    $displaybody = str_get_html($html);
    foreach($displaybody->find('a ') as $element) {
        echo $element->innertext . '<br/>';
    }
    

    If you were referring to PHP's DOMDocument, then its not find() function you need to use, to target each anchor element, you need to use ->getElementsByTagName(), then each selected elements you need to use ->nodeValue:

    $html = "This is some stuff right here. <a href='index.html'>Check this out!</a> <a href=herp.html>And this is another thing!</a> <a href=\"derp.html\">OH MY GOSH</a>";
    
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    foreach($dom->getElementsByTagName('a') as $element) {
        echo $element->nodeValue . '<br/>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分