Reputation: 38
My function:
function raspislinks($url)
{
$chs = curl_init($url);
curl_setopt($chs, CURLOPT_URL, $url);
curl_setopt($chs, CURLOPT_COOKIEFILE, 'cookies.txt'); //Подставляем куки раз
curl_setopt($chs, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($chs, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36 OPR/29.0.1795.60");
curl_setopt($chs, CURLOPT_SSL_VERIFYPEER, 0); // не проверять SSL сертификат
curl_setopt($chs, CURLOPT_SSL_VERIFYHOST, 0); // не проверять Host SSL сертификата
curl_setopt($chs, CURLOPT_COOKIEJAR, 'cookies.txt'); //Подставляем куки два
$htmll = curl_exec($chs);
$pos = strpos($htmll, '<strong><em><font color="green"> <h1>');
$htmll = substr($htmll, $pos);
$pos = strpos($htmll, '<!-- </main>-->');
$htmll = substr($htmll, 0, $pos);
$htmll = end(explode('<strong><em><font color="green"> <h1>', $htmll));
$htmll = str_replace('<a href ="', '<a href ="https://nfbgu.ru/timetable/fulltime/', $htmll);
$GLOBALS['urls'];
preg_match_all("/<[Aa][ \r\n\t]{1}[^>]*[Hh][Rr][Ee][Ff][^=]*=[ '\"\n\r\t]*([^ \"'>\r\n\t#]+)[^>]*>/", $htmll, $urls);
curl_close($chs);
}
How can I use a variable $urls outside the function? It is array. "return $urls"not working or am I doing something wrong. Help me please.
Upvotes: 0
Views: 31
Reputation: 94662
As you load a value into $GLOBALS['urls'];
in the function, you can then use $urls
in code outside this function.
The $GLOBALS
array holds one occurance for each of the variables available in the global scope, so once $GLOBALS['urls'];
is set a value that value can also be referenced as $urls
Like
function raspislinks($url) {
...
//$GLOBALS['urls'];
preg_match_all("/<[Aa][ \r\n\t]{1}[^>]*[Hh][Rr][Ee][Ff][^=]*=[ '\"\n\r\t]*([^ \"'>\r\n\t#]+)[^>]*>/",
$htmll,
$GLOBALS['urls']
);
}
raspislinks('google.com');
foreach ( $urls as $url) {
}
A simpler way would be to put the data in a simple varibale and return it from the function
function raspislinks($url) {
...
//$GLOBALS['urls'];
preg_match_all("/<[Aa][ \r\n\t]{1}[^>]*[Hh][Rr][Ee][Ff][^=]*=[ '\"\n\r\t]*([^ \"'>\r\n\t#]+)[^>]*>/",
$htmll,
$t
);
return $t;
}
$urls = raspislinks('google.com');
foreach ( $urls as $url) {
}
Upvotes: 1