duanchu0031 2011-11-04 04:27 采纳率: 0%
浏览 66
已采纳

从外部网页获取数据

What's the best way to get content from an external website via php?

Using php how do I go to webpage (ex: http://store.domain.com/1/) and scan the HTML coding for data that is found in between (which is the letter C and E). what php method do I use?

<span id="ctl00_ContentPlaceHolder1_phstats1_pname">C</span>
<span id="ctl00_ContentPlaceHolder1_phstats2_pname">E</span>

then save "C" (the found string) to $pname

$_session['pname1'] = $pname1;
$_session['pname2'] = $pname2;
  • 写回答

4条回答 默认 最新

  • dongmaxi6763 2011-11-04 04:52
    关注

    The most efficient method is:

    $content = file_get_contents('http://www.domain.com/whatever.html');
    
    $pos = str_pos($content,'id="c');
    $on=0;
    while($pos!==false)
     {
     $content = substr($content,$pos+4);
     $pos = str_pos($content,'"');
     $list[$on] = substr($content,0,$pos);
     $on++;
     $pos = str_pos($content,'id="c');
     }
    

    Then all yours values will be in the $list array, the count of which is $on.

    You could also do it in one line with one of the preg functions, but I like the old-school method, it's a nanosecond faster.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码