Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

php - extracting anchor values hidden in div tags

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<?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
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...