在 Coldfusion 中反序列化 PHP 数组

发布于 2024-10-08 12:37:36 字数 643 浏览 0 评论 0 原文

我正在做一个 Coldfusion 项目,我需要从 wordpress 支持的数据库中提取一些信息。我需要的一些信息位于存储在 wp_options 表中的序列化数组中。我不知道如何在 Coldfusion 中反序列化数组数据。

我目前正在使用 Coldfusion 8 的开发版本。我无法升级到 Coldfusion 9,因为我的工作应用程序是在 Coldfusion 8 上构建的,

我只能找到此链接 http://www.cfinsider.com/index.cfm/2010/5/4 /Serializing--Deserializing-in-ColdFusion-9 讨论了 CFC 的反序列化,但它似乎不适用于我传递的数组。

这是我尝试反序列化的数据的示例

a:2:{i:2;a:2:{s:5:"title";s:0:"";s:6:"number";i:5;}s:12:"_multiwidget";i:1;}

任何帮助都会很棒。

I'm working on a Coldfusion project where I need to pull some information from a wordpress powered database. Some of the information that I need is in a serialized array stored in the wp_options table. I can't figure out how to deserialize the array data in Coldfusion.

I'm currently using the dev version of Coldfusion 8. I can't upgrade to Coldfusion 9 since my works application is build on Coldfusion 8

I've only been able to find this link http://www.cfinsider.com/index.cfm/2010/5/4/Serializing--Deserializing-in-ColdFusion-9 which talks about deserializing CFC's but it doesn't seem to work on the array I'm passing.

Here is an example of the data I'm trying to deserialze

a:2:{i:2;a:2:{s:5:"title";s:0:"";s:6:"number";i:5;}s:12:"_multiwidget";i:1;}

Any help would be great.

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

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

发布评论

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

评论(4

感性 2024-10-15 12:37:36

您最好的选择可能是查看 Sean Corfield 的 ColdFusion 脚本 项目。我能够用它执行以下操作:

<script:php>
    <?php
        $array = unserialize('a:2:{i:2;a:2:{s:5:"title";s:0:"";s:6:"number";i:5;}s:12:"_multiwidget";i:1;}');
        $_COLDFUSION["test"] = json_encode($array);
    ?>
</script:php>

<cfdump var="#deserializeJSON(variables.test)#">

生成:

alt text

Your best bet might be to check out Sean Corfield's scripting for ColdFusion project. I was able to do the following with it:

<script:php>
    <?php
        $array = unserialize('a:2:{i:2;a:2:{s:5:"title";s:0:"";s:6:"number";i:5;}s:12:"_multiwidget";i:1;}');
        $_COLDFUSION["test"] = json_encode($array);
    ?>
</script:php>

<cfdump var="#deserializeJSON(variables.test)#">

Which produced:

alt text

允世 2024-10-15 12:37:36

如果可以将 PHP 数组序列化为 JSON 字符串,则可以在 CF 端使用 deserializeJson。

If you can serialize the PHP array into a JSON string, you can use deserializeJson on the CF side.

若相惜即相离 2024-10-15 12:37:36

嗯,这是 PHP 序列化的结果 - 我认为 CF 使用完全不同的过程。该模式看起来非常简单:

datatype:size:structure

或者

numbertype:numbervalue

这样

a:2:{i:2;s:3:"foo"}

意味着“大小为 2 的数组 { 整数 2 ; 大小为 3 的字符串 'foo' }”。请注意,数组可以嵌套,并且对象和其他序列化类会使事情变得复杂(请参阅 PHP 序列化对象手册)。

Well, that's a result of PHP serialization - I assume CF uses a completely different process. The schema seems pretty straightforward:

datatype:size:structure

or

numbertype:numbervalue

so

a:2:{i:2;s:3:"foo"}

would mean "array of size 2 { integer 2 ; string 'foo' of size 3 }". Note that arrays can be nested, and things can get complicated with Objects and other serialized classes (see PHP's manual on serializing objects).

半寸时光 2024-10-15 12:37:36

我无法在任何地方找到它,所以我构建了一个解串器。
尝试一下这个要点: https://trycf.com/gist/5a795c5f52b7ad5b18ed62da8eb14f10/lucee5? theme=monokai

这是函数:

public any function deserializePHP(data) {
    if( left( data, 2 ) == "a:"){
        var res = [:];
        var pos = 3;
        var next = find(":", data, pos );
        var val = mid( data, pos, next-pos );
        var pos = next + 2; // ":{" assumed
        var next = find("}", data, pos );
        var val = mid( data, pos, next-pos );
        for (var i=1; i <= listLen(val, ";"); i++) {
            var nextData = listGetAt(val, i, ";");
            if( i mod 2 ){
                var key = deserializePHP(nextData);
            } else {
                var keyVal = deserializePHP(nextData);
                res[key] = keyVal;
            }
        }
    }
    if( left( data, 2 ) == "s:"){
        var res = "";
        var pos = find('"', data ) + 1;
        var next = find('";', data, pos );
        if( next <= 0 ){ next = data.len() };
        var val = mid( data, pos, next-pos );
        res = val;
    }
    if( left( data, 2 ) == "i:"){
        var res = 0;
        var pos = find(':', data ) + 1;
        var next = find(";", data, pos );
        if( next <= 0 ){ next = data.len() + 1 };
        var val = mid( data, pos, next-pos );
        res = val + 0;
    }
    if( isDefined("res")){ return res; } 
    else { return false; }
}

I wasn't able to find one anywhere so I've built a deserializer.
Try it in this gist: https://trycf.com/gist/5a795c5f52b7ad5b18ed62da8eb14f10/lucee5?theme=monokai

Here's the function:

public any function deserializePHP(data) {
    if( left( data, 2 ) == "a:"){
        var res = [:];
        var pos = 3;
        var next = find(":", data, pos );
        var val = mid( data, pos, next-pos );
        var pos = next + 2; // ":{" assumed
        var next = find("}", data, pos );
        var val = mid( data, pos, next-pos );
        for (var i=1; i <= listLen(val, ";"); i++) {
            var nextData = listGetAt(val, i, ";");
            if( i mod 2 ){
                var key = deserializePHP(nextData);
            } else {
                var keyVal = deserializePHP(nextData);
                res[key] = keyVal;
            }
        }
    }
    if( left( data, 2 ) == "s:"){
        var res = "";
        var pos = find('"', data ) + 1;
        var next = find('";', data, pos );
        if( next <= 0 ){ next = data.len() };
        var val = mid( data, pos, next-pos );
        res = val;
    }
    if( left( data, 2 ) == "i:"){
        var res = 0;
        var pos = find(':', data ) + 1;
        var next = find(";", data, pos );
        if( next <= 0 ){ next = data.len() + 1 };
        var val = mid( data, pos, next-pos );
        res = val + 0;
    }
    if( isDefined("res")){ return res; } 
    else { return false; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文