1

The Following code is checking for links within a page and then trying to process those links and check if they work or not, by returning a header code. I basically need to use the returned value $links of the first function in the second one. is it possible?

here is my code

function checkPage ($content){ 
   $textLen = strlen($content);  
   $links = array (); 
   if ( $textLen > 5){ 
      $startPos = 0; 
      $valid = true; 

      while ($valid){ 
         $spos  = strpos($content,'<a ',$startPos); 
         if ($spos < $startPos) $valid = false; 
         $spos     = strpos($content,'href',$spos); 
         $spos     = strpos($content,'"',$spos)+1; 
         $epos     = strpos($content,'"',$spos); 
         $startPos = $epos; 
         $link = substr($content,$spos,$epos-$spos); 
         if (strpos($link,'https://') !== false) $links[] = $link;
         if (strpos($link,'http://') !== false) $links[] = $link;
      } 
   } 
  return **$links**; 
};

print_r(checkPage($content));

foreach ($links as &$link ) {
$ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $link);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
   $data = curl_exec($ch);
   $headers = curl_getinfo($ch);
   if(curl_error($ch))
{
    echo 'error:' . curl_error($ch);
}   
   curl_close($ch);
   echo $link." returns code ".$headers['http_code']."<br />";  
};
4
  • return **$links**;? Do you mean that you simply want to store the value from the checkPage() function? This is PHP101... $links = checkPage($content);. Commented Jul 17, 2017 at 13:51
  • Do you just need $links = checkPage($content); ? That looks to be missing from your code, but that's very basic PHP considering what you already have. Commented Jul 17, 2017 at 13:55
  • Yes, I forgot to copy the variable "content" that contain a piece of HTML markup. Return $links; should be displayed without the asterisks as I tried to bold the word on the page editor here. Commented Jul 17, 2017 at 13:56
  • I would highly recommend going through some basic PHP-tutorials Commented Jul 17, 2017 at 13:57

1 Answer 1

2

You're printing the returned value, but then ignoring it after that:

print_r(checkPage($content));

Store the returned value in a variable. It doesn't need to be the same variable name. And in this case it probably shouldn't be the same variable name to keep the concept clear. So something like this:

$returnedLinks = checkPage($content);
print_r($returnedLinks);

foreach ($returnedLinks as $link ) {
    // do something with $link
}

Returning a value from a function doesn't make the variable itself available outside the function. It just means that when you call the function then that function call evaluates to a result, just as if you defined that result in-line instead of calling a function. You just need to store that result in a variable to use it, just like any other value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'm still new around coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.