Function loop returns only one value

Asked

Viewed 100 times

1

I’m trying to get data from an online page only the code is presenting a problem, it can not process all items in the way POST, within the foreach.

Even if I put the items in list only takes the last item posted. I wonder how I can solve this problem.

Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<form id="form1" name="form1" method="post" action="pegavideo.php">
  <p>
    <textarea name="urls" id="urls" cols="100" rows="20"></textarea>
  </p>
  <p>
    <input type="submit" name="gerar" id="gerar" value="Gerar urls do Super Animes" />
  </p>
</form>
<?php 

function pegarDados ($valorUrl) {
   $url = $valorUrl;
   $html = file_get_contents($url);
   $doc = new DOMDocument();
   @$doc->loadHTML($html);
   $tags = $doc->getElementsByTagName('source');
   foreach ($tags as $tag) { return $tag->getAttribute('src').'<br/>';}
}


$dados = $_POST['urls'];
$valoresUrl = explode("\n",$dados);
for($i = 0; $i < count($valoresUrl); $i++) {
   echo pegarDados($valoresUrl[$i]);
}
?>
</body>
</html>
  • 1

    You are taking the n with the Trim and trying to make the explode with an n that you removed earlier

  • Ready corrected that part you mentioned.

  • What error you are receiving

  • What is happening and the following, if I put a list of links, only returns a single result. That is if I put for example 10 links only returns a single url.

  • Make a $valoresUrl print_r to see what’s on for

  • I put the numbers 1,2,3 to the test and gave this result Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )

  • Try putting 3 URL separated by enter which gives the print_r

  • Array ( [0] => https://drive.google.com/1 [1] => https://drive.google.com/2 ) Array ( [0] => https://drive.google.com/1 [1] => https://drive.google.com/2 ) Literally repeating the same result.

Show 3 more comments

1 answer

2


Here’s a problem:

 function pegarDados ($valorUrl) {
    $url = $valorUrl;
    $html = file_get_contents($url);
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $tags = $doc->getElementsByTagName('source');
    /// este foreach nunca vai sair da primeira iteração:
    foreach ($tags as $tag) { return $tag->getAttribute('src').'<br/>';}
 }

When the foreach happen, the return will already leave the function returning only the first src. If you want to return a list separated by <br/> needs something like this:

   ...
   $tags = $doc->getElementsByTagName('source');
   $lista = '';
   foreach ($tags as $tag) { $lista .= $tag->getAttribute('src').'<br/>';}
   return $lista;
 }

In this way, the foreach to each iteration will add the src desired in $list, and after the loop, return the variable $lista loop-fed.


Remarks:

  • The tag should probably be <br>. Use <br\> only makes sense if you are making a legacy page in XHTML, or have any reason to process the output in XML.

  • When it is to run an instruction only in the loop, it can simplify without the { }:

    foreach ($tags as $tag) $lista .= $tag->getAttribute('src').'<br>';
    

    Of course you must remain true to the style you have adopted for the rest of the code.

  • Here you duplicate the variable seemingly aimlessly:

    $url = $valorUrl;
    $html = file_get_contents($url);
    

    Could just do:

    $html = file_get_contents($valorUrl);
    
  • Instead of this deletion of errors in @$doc->loadHTML($html);, maybe it would be better to treat the error conditionally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.