【问题标题】:In PHP xmlparser, why can't I store a global from my character_data_handler()?在 PHP xmlparser 中,为什么我不能从我的 character_data_handler() 存储一个全局变量?
【发布时间】:2010-03-02 20:39:45
【问题描述】:

(或者这里是代码要点:

$host = "";
...
xml_set_character_data_handler($xmlparser, "tagContents");
...
function tagContents($parser, $data) { 
    global $current; 
    global $host;
    if ($current == "HOST") { 
        $host = $data;         // Trying to store a global here
    }
    if ($current == "PATH") { 
        echo $host.$data;      // But its null when I get here.  WHY??
    }
}

我正在尝试像这样将路径附加到主机以创建单行 URL,因为 xmlparse 在每个回显之后都会添加一个换行符。因此,如果有人能告诉我如何防止换行符,那也可以解决我的问题!

顺便说一句:

  • 我也尝试过引用超级全局 $GLOBALS['host'] 得到相同的结果
  • 我的主机服务器只有 PHP4 可用 (otherwise I'd use SimpleXML)

谢谢, 鲍勃

【问题讨论】:

    标签: php global-variables xml-parsing


    【解决方案1】:

    尝试使用超全局 $GLOBALS['host'] 无论如何它更快。这是你的固定代码

    $host = "";
    ...
    xml_set_character_data_handler($xmlparser, "tagContents");
    ...
    function tagContents($parser, $data) 
    { 
        global $current; 
    
        if ($current == "HOST") { 
            $GLOBALS['host'] = $data;         // Trying to store a global here
        }
        if ($current == "PATH") { 
            echo $GLOBALS['host'].$data;      
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 2021-01-14
      相关资源
      最近更新 更多