本文介绍了提取隐藏在div标签中的锚点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在html页面中,我需要从所有锚点链接中提取v的值...每个锚链接都隐藏在一些5个div标签中

   

每个v值都有11个字符,因为现在我试图逐个字符地读取它像

 <?php 
$ file = fopen(xx.html,r)或退出(无法打开文件!);
$ d ='v';
$ dd ='=';
$ vd = array(); $(!feof($ file))

{
$ f = fgetc($ file);
if($ f == $ d)
{
$ ff = fgetc($ file);
if($ ff == $ dd)
{
$ idea ='';
for($ i = 0; $ i {
$ sData = fgetc($ file);
$ id = $ id。$ sData;
}
array_push($ vd,$ id);

这就是获取v的每个字符并将它存储在sData变量中并将其推入id,将这11个字符作为字符串(id)...
,问题是......通过整个html文件搜索'v =',如果发现读取11个字符并将其推入sData数组中,需要相当长的时间......所以请帮助我完善这些东西。

?php
函数substring(& $ string,$ start,$ end)
{
$ pos = strpos(>。$ string,$ start);
如果(!$ pos)返回;
$ pos--;
$ string = substr($ string,$ pos + strlen($ start));
$ posend = strpos($ string,$ end);
$ toret = substr($ string,0,$ posend);
$ string = substr($ string,$ posend);
返回$ toret;
}
$ contents = @file_get_contents(xx.html);

$ old =;
$ videosArray = array();
while($ old<> $ contents)
{
$ old = $ contents;
$ v = substring($ contents,?v =,&);
if($ v)$ videosArray [] = $ v;
}

// $ videosArray是v $
的数组?>


From a html page I need to extract the values of v from all anchor links…each anchor link is hidden in some 5 div tags

<a href="/watch?v=value to be retrived&amp;list=blabla&amp;feature=plpp_play_all">

Each v value has 11 characters, for this as of now am trying to read it by character by character like

<?php
$file=fopen("xx.html","r") or exit("Unable to open file!");
$d='v';
$dd='=';
$vd=array();
while (!feof($file))
  {
  $f=fgetc($file);
  if($f==$d)
  {
  $ff=fgetc($file);
  if ($ff==$dd)
  {
  $idea='';
  for($i=0;$i<=10;$i++)
  {
$sData = fgetc($file);
$id=$id.$sData;
  }
  array_push($vd, $id);

That is am getting each character of v and storing it in sData variable and pushing it into id so as to get those 11 characters as a string(id)…the problem is…searching for the ‘v=’ through the entire html file and if found reading the 11characters and pushing it into a sData array is sucking, it is taking considerable amount of time…so pls help me to sophisticate the things

解决方案
<?php
function substring(&$string,$start,$end)
{
    $pos = strpos(">".$string,$start);
    if(! $pos) return "";
    $pos--;
    $string = substr($string,$pos+strlen($start));
    $posend = strpos($string,$end);
    $toret = substr($string,0,$posend);
    $string = substr($string,$posend);
    return $toret;
}
$contents = @file_get_contents("xx.html");

$old="";
$videosArray=array();
while ($old <> $contents)
{
$old = $contents;
$v = substring($contents,"?v=","&");
if($v) $videosArray[] = $v;
}

//$videosArray is array of v's
?>

这篇关于提取隐藏在div标签中的锚点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:04