在php中将对象字符串的打印数组转换为json

在php中将对象字符串的打印数组转换为json,php,Php,我的一名团队成员已将该对象保存在Mysql数据库中。我需要提取对象属性值。当我试图使用PHP从Mysql获取它时,我将其作为字符串获取。除了使用PHPsubstr()函数之外,直接访问属性也没有什么好处 是否有任何选项将保存在数据库中的以下字符串再次转换为对象,以便我可以访问它的属性 stdClass Object ( [status] => 0 [environment] => Sandbox [receipt] => stdClass Object (

我的一名团队成员已将该对象保存在Mysql数据库中。我需要提取对象属性值。当我试图使用PHP从Mysql获取它时,我将其作为字符串获取。除了使用PHP
substr()
函数之外,直接访问属性也没有什么好处

是否有任何选项将保存在数据库中的以下字符串再次转换为对象,以便我可以访问它的属性

stdClass Object
(
[status] => 0
[environment] => Sandbox
[receipt] => stdClass Object
    (
        [quantity] => 1
        [expires_date] => 2017-04-18 08:56:57 Etc/GMT
        [is_trial_period] => false
        [purchase_date] => 2017-04-18 08:51:57 Etc/GMT
        [product_id] => com.1monthAuto.baseball
        [original_purchase_date_pst] => 2017-04-18 01:51:58 America/Los_Angeles
        [original_purchase_date_ms] => 1492505518000
        [web_order_line_item_id] => 1000000034854560
        [original_purchase_date] => 2017-04-18 08:51:58 Etc/GMT
    )

[latest_receipt] => MIIT6wYJKoZIhvcNAQcCoIIT3DCCE9gC
)

试试这个,希望它能帮助你

正则表达式:
/latest\u receipt\]\s*=>\s*\K[^\s\)]+//code>

最新收据\]\s*=>\s*
这将匹配最新收据]空格=>空格

\K
将重置以前的匹配

[^\s\)]+
这将匹配除空格(
\s
)和


使用正则表达式和str_replace将最新的_收据内容作为字符串:

$dictionary="stdClass Object
    (
    [status] => 0
    [environment] => Sandbox
    [receipt] => stdClass Object
        (
            [quantity] => 1
            [expires_date] => 2017-04-18 08:56:57 Etc/GMT
            [is_trial_period] => false
            [purchase_date] => 2017-04-18 08:51:57 Etc/GMT
            [product_id] => com.1monthAuto.baseball
            [original_purchase_date_pst] => 2017-04-18 01:51:58 America/Los_Angeles
            [original_purchase_date_ms] => 1492505518000
            [web_order_line_item_id] => 1000000034854560
            original_purchase_date] => 2017-04-18 08:51:58 Etc/GMT
        )

    [latest_receipt] => MIIT6wYJKoZIhvcNAQcCoIIT3DCCE9gC
    )";
preg_match('/.*\[latest_receipt\].*$/m', $dictionary, $matches);
$receipt = str_replace('[latest_receipt] => ', '', $matches[0]);
echo $receipt;
您可以将其设置为函数,并用变量替换最新的_收据,以提取其他数据


但是你真的应该考虑将其存储为json对象,或者创建新列来存储此数据。

你想访问哪个属性?在数据库中存储对象对我来说是一种糟糕的设计模式,因为它会导致类似的问题。我想访问
最新收据
。你确定这就是字符串吗?那么有人正在数据库中存储打印的输出?那太可怕了。无论如何是时候学习如何编写解析器了,也许还有一些正则表达式。我想没有人会直接为你写的您应该在保存或类似操作之前对数据进行
json\u编码。或者编写一个解析器,得到可能不一致的结果。是否有任何选项可用于将此字符串设置为对象,以便我也可以提取其他属性值?@ArunJain我创建了一个新的解决方案,我将发布该解决方案,希望它能帮助您
<?php
ini_set('display_errors', 1);
/**
 * @author Sahil Gulati <sahil.gulati1991@outlook.com> 
 */
echo printr_source_to_json(
        print_r(
                array("Name"=>"Sahil Gulati",
                      "Education"=>array(
                          "From"=>array(
                              "DU"=>array(
                                  "Course"=>"B.Sc. (Hons.) Computer Science.")
                              )
                          )
                    )
                , true
                )
        );
/**
 * This function will convert output string of `print_r($array)` to `json string`
 * @note Exceptions are always there i tried myself best to get it done. Here $array can be array of arrays or arrays of objects or both
 * @param String $string This will contain the output of `print_r($array)` (which user will get from ctrl+u of browser),
 * @return String
 */
function printr_source_to_json($string)
{
    /**
     *replacing `stdClass Objects (` to  `{`
     */
    $string = preg_replace("/stdClass Object\s*\(/s", '{  ', $string);

    /**
     *replacing `Array (` to  `{`
     */
    $string = preg_replace("/Array\s*\(/s", '{  ', $string);
    /**
     *replacing `)\n` to  `},\n`
     * @note This might append , at the last of string as well 
     * which we will trim later on.
     */
    $string = preg_replace("/\)\n/", "},\n", $string);

    /**
     *replacing `)` to  `}` at the last of string
     */
    $string = preg_replace("/\)$/", '}', $string);
    /**
     *replacing `[ somevalue ]` to "somevalue"
     */
    $string = preg_replace("/\[\s*([^\s\]]+)\s*\](?=\s*\=>)/", '"\1" ', $string);
    /**
     * replacing `=> {`  to `: {`
     */
    $string = preg_replace("/=>\s*{/", ': {', $string);
    /**
     * replacing empty last values of array special case `=> \n }` to : "" \n
     */
    $string = preg_replace("/=>\s*[\n\s]*\}/s", ":\"\"\n}", $string);

    /**
     * replacing `=> somevalue`  to `: "somevalue",` 
     */
    $string = preg_replace("/=>\s*([^\n\"]*)/", ':"\1",', $string);
    /**
     * replacing last mistakes `, }` to `}` 
     */
    $string = preg_replace("/,\s*}/s", '}', $string);
    /**
     * replacing `} ,` at the end to `}`
     */
    return $string = preg_replace("/}\s*,$/s", '}', $string);
}
$dictionary="stdClass Object
    (
    [status] => 0
    [environment] => Sandbox
    [receipt] => stdClass Object
        (
            [quantity] => 1
            [expires_date] => 2017-04-18 08:56:57 Etc/GMT
            [is_trial_period] => false
            [purchase_date] => 2017-04-18 08:51:57 Etc/GMT
            [product_id] => com.1monthAuto.baseball
            [original_purchase_date_pst] => 2017-04-18 01:51:58 America/Los_Angeles
            [original_purchase_date_ms] => 1492505518000
            [web_order_line_item_id] => 1000000034854560
            original_purchase_date] => 2017-04-18 08:51:58 Etc/GMT
        )

    [latest_receipt] => MIIT6wYJKoZIhvcNAQcCoIIT3DCCE9gC
    )";
preg_match('/.*\[latest_receipt\].*$/m', $dictionary, $matches);
$receipt = str_replace('[latest_receipt] => ', '', $matches[0]);
echo $receipt;