duanmeng3573 2014-09-28 20:36
浏览 17
已采纳

如何在此函数之外使用$ matches变量?

I am trying to get the variable $matches to be usable outside of this function. So I can use echo $matches[0][0]; or $matches[0][1]; after the function gets used in my document. So far I haven't been able to use the matches variable outside of the function at all.

    function curlLink($url, $regex)
{
        include ('lib/dBug.php');
        require_once('lib/curl_http_client.php');
        $curl = &new Curl_HTTP_Client();
        //$useragent = "Googlebot/2.1 (+http://www.google.com/bot.html)";  
        $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0";
        $curl->set_user_agent($useragent);
        ini_set('max_execution_time','0');
        $x=0;
        $matches = array();
        while (sizeof($matches) == 0 && $x < 15) {
            $html_data = $curl->fetch_url($url);
            preg_match_all($regex, $html_data, $matches); 
            $x++;
            array_shift($matches);
        }
        if (empty($matches[0][0])) {
            echo '<img src=\"/img/bigbrokenlink.png\" /><br /><br />
            <b>Sorry, no results from your search!</b><br />';
        }
        if (!empty($matches[0][0])) {
            //return $matches; //This doesn't seem to return a usable variable...
            $dBug = new dBug ($matches);
        }
}
  • 写回答

3条回答 默认 最新

  • douwo4837 2014-09-28 20:38
    关注

    declare $matches = array(); outside of function as global variable.or return it to some function and use.

    e.g.:

    global $matches;
    function curlLink($url, $regex)
    {
        global $matches;
    // implementation 
    }
    

    //access here $matches;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序