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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?