从 PHP 中的 SimpleXMLElement 获取 iso8601.time 标签

发布于 2024-09-30 14:12:40 字数 1427 浏览 3 评论 0原文

我有以下功能。我想添加对 iso8601 时间格式的支持,但我无法让它工作。因为我在 php 中无法执行 (string)$tag->iso8601.time 。有没有办法获取 iso8601.time 元素? 标签是一个SimpleXMLElement

private function _tagToPhpType($tag) {
        /*
         *  <i4> or <int>   four-byte signed integer    -12
         *  <boolean>   0 (false) or 1 (true)   1
         *  <string>    string  hello world
         *  <double>    double-precision signed floating point number   -12.214
         *  <dateTime.iso8601>  date/time   19980717T14:08:55
         *  <base64>    base64-encoded binary   eW91IGNhbid0IHJlYWQgdGhpcyE=
         *       
         *  Source: http://www.xmlrpc.com/spec
         */

        if(!empty($tag->string)) {
            return (string)$tag->string;
        }
        elseif(!empty($tag->int)) {
            return (int)$tag->int;
        }
        elseif(!empty($tag->i4)) {
            return (int)$tag->i4;
        }
        elseif(!empty($tag->boolean)) {
            return (bool)$tag->boolean;
        }
        elseif(!empty($tag->double)) {
            return (double)$tag->double;
        }
        elseif(!empty($tag->base64)) {
            // @todo: Decode BASE64
            return (int)$tag->base64;
        } // @todo: Add iso8601 time type
        else {
            return (string)$tag;
        }
    }

I have the function bellow. I want to add support for iso8601 time format but I just can't get it to work. Since I in php can't do (string)$tag->iso8601.time. Is there a way to get the iso8601.time element? The tag is a SimpleXMLElement.

private function _tagToPhpType($tag) {
        /*
         *  <i4> or <int>   four-byte signed integer    -12
         *  <boolean>   0 (false) or 1 (true)   1
         *  <string>    string  hello world
         *  <double>    double-precision signed floating point number   -12.214
         *  <dateTime.iso8601>  date/time   19980717T14:08:55
         *  <base64>    base64-encoded binary   eW91IGNhbid0IHJlYWQgdGhpcyE=
         *       
         *  Source: http://www.xmlrpc.com/spec
         */

        if(!empty($tag->string)) {
            return (string)$tag->string;
        }
        elseif(!empty($tag->int)) {
            return (int)$tag->int;
        }
        elseif(!empty($tag->i4)) {
            return (int)$tag->i4;
        }
        elseif(!empty($tag->boolean)) {
            return (bool)$tag->boolean;
        }
        elseif(!empty($tag->double)) {
            return (double)$tag->double;
        }
        elseif(!empty($tag->base64)) {
            // @todo: Decode BASE64
            return (int)$tag->base64;
        } // @todo: Add iso8601 time type
        else {
            return (string)$tag;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-10-07 14:12:40

如手册 (SimpleXML 基本用法) 和相关示例 (#3) 中所述:

访问 XML 文档中包含 PHP 命名约定不允许的字符(例如连字符)的元素可以通过将元素名称封装在大括号和撇号内来完成。

因此,在您的情况下 $tag->{'dateTime.iso8601'} 将获得 元素。

(您的 iso8601.time 与代码中的注释不匹配,但如果您需要获取该标签,那么从上面的答案应该很容易算出。)

As noted in the manual (SimpleXML basic usage) and the associated example (#3):

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

So in your case $tag->{'dateTime.iso8601'} will get an <dateTime.iso8601> element.

(Your iso8601.time doesn't match the comment in the code, though if you need to get that tag then it should be easy to work out from the answer above.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文