Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match_all返回错误未定义的偏移量_Php_Regex_Preg Match All - Fatal编程技术网

Php preg_match_all返回错误未定义的偏移量

Php preg_match_all返回错误未定义的偏移量,php,regex,preg-match-all,Php,Regex,Preg Match All,我正在使用preg_match_all并返回此错误: Notice: Undefined offset: 0 in B:\xampp\htdocs\fogsy\link_searcher.php on line 98 这是第98行: $server_name=$matches[0][1]."/"; 以下是我的功能: 它用于从html正文中检索链接 function GetLinks($body_str,$parent_url) {

我正在使用preg_match_all并返回此错误:

Notice: Undefined offset: 0 in B:\xampp\htdocs\fogsy\link_searcher.php on line 98
这是第98行:

$server_name=$matches[0][1]."/";
以下是我的功能: 它用于从html正文中检索链接

    function GetLinks($body_str,$parent_url)
        {           
            $url_list=array();

            preg_match_all('/http:\/\/(.*)\//iU', $parent_url, $matches, PREG_SET_ORDER);
            $server_name=$matches[0][1]."/";

            preg_match_all('/< *a.*href *= *[\'"](.*)[\'"].*>(.*)< *\/a *>/iU', $body_str, $matches, PREG_SET_ORDER);

            for($count=0;$count<count($matches);$count++)
                {
                    $text=$matches[$count][2];

                    if(strpos(strtolower($matches[$count][1]),"http://")===false&&strpos(strtolower($matches[$count][1]),"www")===false)
                    $href="http://".$server_name.trim($matches[$count][1],"/");                         
                    else $href=$matches[$count][1];                             

                    $url_list[$text."_".$count]=$href;  
                }   
            return $url_list;
        }
函数GetLinks($body\u str,$parent\u url) { $url_list=array(); preg\u match\u all('/http:\/\/(.*)\//iU',$parent\u url,$matches,preg\u SET\u ORDER); $server_name=$matches[0][1]。“/”; preg\u match\u all('/<*a.*href*=*[\'”](.*[\'”].>(.*)<*\/a*>/iU',$body\u str,$matches,preg\u SET\u ORDER);
对于($count=0;$count这是因为这里没有索引0:
$server\u name=$matches[0][1]。“/”;

打印($matches);然后查看

之所以发生这种情况,是因为
$parent\u url

更改如下:

if(count($matches) > 0){
    $server_name=$matches[0][1]."/";
}
else{
    $server_name="";
}
但这会影响其他功能,最好向此功能发送格式良好的
$parent\u url