我正在尝试使用php搜索并检查数据库中可用的重复详细信息.用户输入多个名称,然后输入电话号码以检查重复项.以下是我的功能.我刚刚裁掉一些零件,因为它太长了.
function gtc($names,$phone)
{
$pageNumb=20;
$position = array(5);
$sepname=explode(",","$names");
foreach ($sepname as $sepname1)
{
for ($page=0;$page<=$pageNumb;$page=$page + 1)
{
$turl="http://192.168.111.2119/search.php?qry=$sepname1&page=$page";
$search=curl_init();
curl_setopt($search,CURLOPT_URL,$turl);
curl_setopt($search,CURLOPT_RETURNTRANSFER,1);
curl_setopt($search,CURLOPT_FAILONERROR,true);
curl_setopt($search,CURLOPT_AUTOREFERER,true);
$result=curl_exec($search);
$dom = new DOMDocument();
@$dom->loadHTML($result);
$xpath=new DOMXPath($dom);
$elements = $xpath->evaluate("//div[@id='inf']");
foreach ($elements as $element)
{
$position[$sepname1] = $position[$sepname1] + 1;
foreach($position as $key=>$val)
$contct = $element->getElementsByTagName("contact")->item(0)->nodeValue;
if (preg_match("/$phone/i",$contct)) {
echo "Found In Database>";
}
}
ob_flush();
flush();
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
该函数完美地工作,但我遇到的唯一问题是虽然找到匹配,但它会一直持续到for循环中给出的最后一页,然后通过下一个名称.我想知道是否有可能在找到匹配时停止处理,然后搜索下一个名称?
我不想使用exit(); 因为它完全停止了执行
输入格式如下
用户输入名称:john,Sam,Michael,Steve
电话号码:0084554741
任何帮助将不胜感激
Gor*_*don 11
你可能正在寻找 break
break结束当前for,foreach,while,do-while或switch结构的执行.
// output numbers 1,2,3,4
foreach( range(1,10) as $number) {
if($number === 5) break;
echo $number, PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
和/或 continue
continue在循环结构中用于跳过当前循环迭代的其余部分,并在条件评估和下一次迭代开始时继续执行.
// output only even numbers
foreach( range(1,10) as $number) {
if($number % 2) continue;
echo $number, PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)