调整PHP代码从给定的URL中检索DOM [英] Adjust PHP code Retrieve the DOM from a given URL

查看:75
本文介绍了调整PHP代码从给定的URL中检索DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,使用以下代码从URL中检索DOM
所有A标签并打印它们的HREF
现在我的输出包含A我不希望它出现在这里
http://trend.remal .com / parsing.php



我需要在 http://twitter.com/namehere



如此输出namehere的打印列表

  include('simple_html_dom.php'); 

//从给定的URL中检索DOM
$ html = file_get_html('http://tweepar.com/sa/1/');
$ urls = array();

foreach($ html-> find('a')as $ e)
{
//如果是twitter链接
if(strpos($ e> href,'://twitter.com/')!== false)
{
//我们没有在数组中存在
if(! in_array($ urls,$ e-> href))
{
//将它添加到我们的数组
$ urls [] = $ e-> href;




echo implode('< br>',$ urls);

echo $ e-> href。 <峰; br>;


解决方案不是简单地使用 $ urls [] = $ e-> href ,使用一个匹配正则表达式的用户名:

  preg_match('〜twitter.com /(.+)〜', $ e-> href,$ matches); 
$ urls [] = $ matches [1];


Hello ,

im using the following code to Retrieve the DOM from URL all "A" tags and print their HREFs Now my output is contain "A" i dont want its my out is here http://trend.remal.com/parsing.php

i need to clear my out to be only the name after http://twitter.com/namehere

so output print list of "namehere"

    include('simple_html_dom.php');

 // Retrieve the DOM from a given URL
 $html = file_get_html('http://tweepar.com/sa/1/');
 $urls = array();

  foreach ( $html->find('a') as $e )
  {
  // If it's a twitter link
  if ( strpos($e->href, '://twitter.com/') !== false )
  {
    // and we don't have it in the array yet
    if ( ! in_array($urls, $e->href) )
    {
        // add it to our array
        $urls[] = $e->href;
    }
   }
   }

  echo implode('<br>', $urls);

echo $e->href . '<br>';

解决方案

Instead of simply using $urls[] = $e->href, use a regex to match the username:

preg_match('~twitter.com/(.+)~', $e->href, $matches);
$urls[] = $matches[1];

这篇关于调整PHP代码从给定的URL中检索DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆