[英]How do I create an infinite function loop? (PHP)
Here's the actual function 这是实际功能
$linkArray = array(//associative array for title and url of site and etc
"url"=>"",
"title"=>"",
"num"=>""
);
$site = $_POST['link'];
function parseLink($link){
global $conn;//reinitialize conn variable b/c inside fn it's unknown unless reinitialized
//get title of page
$page_title = get_url_title($link);
//1-Get HTML content
$html = file_get_contents($link);
$a_tag = preg_match_all('/<a[^>]+>/i', $html, $result);
$i = 0;//dummy var
//check if we get result
if($a_tag > 0){
while($i < $a_tag){
$alink = $result[0][$i];
preg_match_all('/(href)=("[^"]*")/i',$alink, $href);
$hreflnk = $href[0][0];
$hreflnk = str_replace('href="',"",$hreflnk);
$hreflnk = substr($hreflnk, 0, -1);
//filter out only links that start with http -> this will automatically include https
//$randomvar = preg_match("/^http/i",$hreflnk,$outp);
if(preg_match("/^http/i",$hreflnk)){
$hreflnk = $hreflnk;
} else{
//idk
$hreflnk = '';
}
$linkArray['url'][$i] = $hreflnk;
$linkArray['title'] = $page_title;
$linkArray['num'] = $a_tag;
$siteTitle = $linkArray['title'];
$numLinks = $linkArray['num'];
$siteUrl = $linkArray['url'][$i];
$queryI = $conn->query("INSERT INTO data (title, url, numLinks) VALUES ('$siteTitle','$siteUrl','$numLinks')");
$outLink = $linkArray['url'][0];
$i++;
}
}
return $outLink;
}
This function takes in a URL searches through the HTML and stores all http
links in array and later uploads them to a db. 此函数接受通过HTML进行的URL搜索,并将所有
http
链接存储在数组中,然后将其上载到数据库。 Before it ends it returns the first link of all of those that were collected. 在结束之前,它返回所有已收集链接的第一个链接。 So what I want to do is run that link back into this function.
因此,我想做的就是将该链接重新运行到此函数中。 Then this process will repeat.
然后,将重复此过程。
Just use a recursive function: 只需使用一个递归函数:
function foo($i = 0) {
if ($i == 100) {
return 'yay';
} else {
$i++;
return foo($i);
}
}
foo();
Alternatively, do you really need a repeating function? 另外,您真的需要重复功能吗?
$i = 0;
while($i != 100) {
$i++;
}
or: 要么:
$i = 0;
do {
$i++;
} while ($i != 100);
With a string: 用一个字符串:
do {
$recordString = DB::get()->getRow('SELECT `name` from `username` ORDER BY RAND()')->getField('name');
} while ($recordString != 'admin') {
//$recordString will now 100% be 'admin', unless you time out during the execution
Edit : 编辑 :
With your new question update, I'd propose you to change your code to the following: 随着新问题的更新,建议您将代码更改为以下内容:
<?php
function parseLink($link, $outLinks = array()){
global $conn;//reinitialize conn variable b/c inside fn it's unknown unless reinitialized
//get title of page
$page_title = get_url_title($link);
//1-Get HTML content
$html = file_get_contents($link);
$document = DOMDocument::loadHTML($html);
foreach ($document->getElementsByTagName('a') as $element) {
$href = $element->getAttribute('href');
if (!isset($outLinks[$href])) { //don't redo ones we've already done
$linkArray['url'][$i] = $href;
$linkArray['title'] = $page_title;
$linkArray['num'] = $i;
$siteTitle = $linkArray['title'];
$numLinks = $linkArray['num'];
$siteUrl = $linkArray['url'][$i];
$queryI = $conn->query("INSERT INTO data (title, url, numLinks) VALUES ('$siteTitle','$siteUrl','$numLinks')");
$outLinks[$href] = $linkArray['url'][0];
$i++;
}
}
foreach ($outLinks as $href => $dbID) {
$outsLinks = parseLink($href, $outLinks);
}
return $outLinks;
}
?>
Changes : 变化 :
DOMDocument
instead, as regex shouldn't be used to parse HTML. DOMDocument
,因为正则表达式不应用于解析HTML。 $outLink
to use the array $outLinks
which it returns. $outLink
更改$outLink
使用它返回的数组$outLinks
。 while($myvar) $myvar = test($myvar);
Please try this: 请尝试以下方法:
$value = "Initial value";
function test($input){
// does stuff inside
// sets output to false if blank
return $output;
}
while($value) {
// runs until test returns false or an empty string
$value = test($value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.