使用PHP从文本字符串中提取值?

时间:2012-07-17 15:21:37

标签: php string extract recurly

使用Recurly API,返回以下文本。我想弄清楚的是用PHP从这个字符串中提取值并将它们放入PHP变量中的最简单方法。

我已经在PHP手册中查看了各种方法来查找文本,但是在我发现它们的位置对我来说仍然不明显之后,我真正了解了实际获取值的确切过程。

例如,如何提取plan_code的值并删除值周围的引号,以便结果为$plan = 'starter'

  

Recurly_Subscription [HREF = HTTPS://api.recurly.com/v2/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxx]   帐户= LT; Recurly_Stub [帐户]   HREF = HTTPS://api.recurly.com/v2/accounts/1> ;,   activated_at =“2012-07-14 12:55:47 +00:00”,currency =“USD”,   current_period_ends_at =“2012-08-14 12:55:47 +00:00”,   current_period_started_at =“2012-07-14 12:55:47 +00:00”,   计划=“Recurly_Plan [HREF = HTTPS://api.recurly.com/v2/plans/starter]   name =“Starter”,plan_code =“starter”,   setup_fee_in_cents =“< Recurly_CurrencyList []>”,   unit_amount_in_cents =“< Recurly_CurrencyList []>”>“,   quantity = 2,state =“active”,subscription_add_ons = [],   unit_amount_in_cents = 1200,   UUID = “1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3”

4 个答案:

答案 0 :(得分:3)

您可以使用Recurly的PHP API:

$result = Recurly_js::fetch($_POST['recurly_token']);
$plan = $result->plan_code;

令牌是订阅对象。然后,您可以检索有关该帐户及其结算信息的其他信息,如下所示:

$account = $result->account->get();
$billing = $account->billing_info->get();

从帐户中检索字段(例如帐户持有人的名字)及其结算信息(例如信用卡记录的最后4位数字),与上面提取的plan_code方式相同。

在Recurly的文档中,执行上述操作要比查找方法更容易!

答案 1 :(得分:2)

一个技巧是将其转换为查询字符串语法并使用parse_str(original,array)。

$original = 'Recurly_Subscription[href=https://api.recurly.com/v2/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxx] account=, activated_at="2012-07-14 12:55:47 +00:00", currency="USD", current_period_ends_at="2012-08-14 12:55:47 +00:00", current_period_started_at="2012-07-14 12:55:47 +00:00", plan="Recurly_Plan[href=https://api.recurly.com/v2/plans/starter] name="Starter", plan_code="starter", setup_fee_in_cents="", unit_amount_in_cents="">", quantity=2, state="active", subscription_add_ons=[], unit_amount_in_cents=1200, uuid="1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3"';

$newstring = str_replace(", ", "&", $original);  // turns into querystring format &var=val
// you may need to use addslashes and/or replace the double quotes surrounding vars.

接下来,您使用parse_str并构建一个名称/值的关联数组:

parse_str($newstring, $myarray);

然后你可以打印出来,看看它的外观和调整:

print_r($myarray);

答案 2 :(得分:2)

有几种方法可以解决这个问题。

可能是最简单的一个:如果您可以获得更符合标准的结果,请查看API。 PHP具有处理XML,JSON和其他几种格式的内置函数,因此您可以获得可以访问信息位的数组或对象。

使用此格式执行此操作的一种可能方法: 在,拆分,再次在=拆分以获取密钥=>价值对。 (你可能需要剪掉第一行,因为之后我没有看到,。) 草图粗略,未经测试:

<?php $result = your_api_call();
$chunks = explode(',', $result);
$resultAsArray = array();
foreach ($chunks as $biggerPart) {
   $parts = explode('=', $biggerPart);
   $tmpKey = trim($parts[0]);
   $tmpValue = str_replace('"', '', $parts[1]);
   $resultAsArray[$tmpKey] = $tmpValue;
}

echo $resultAsArray['plan_code'];

答案 3 :(得分:1)

    function value($string, $s) {
        $pos = strpos($string, $s);
        if($pos!==FALSE) {
            $pos += strlen($s) + 2;
            $fpos = $pos;
            while ($string[$fpos] != '"') {
                ++$fpos;
            }
            $result = substr($string, $pos, $fpos-$pos);
        } else {
            $result = '';
        }
        return $result;
    }

    $string = 'Recurly_Subscription[href=https://api.recurly.com/v2/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxx] account=<Recurly_Stub[account] href=https://api.recurly.com/v2/accounts/1>, activated_at="2012-07-14 12:55:47 +00:00", currency="USD", current_period_ends_at="2012-08-14 12:55:47 +00:00", current_period_started_at="2012-07-14 12:55:47 +00:00", plan="Recurly_Plan[href=https://api.recurly.com/v2/plans/starter] name="Starter", plan_code="starter", setup_fee_in_cents="<Recurly_CurrencyList []>", unit_amount_in_cents="<Recurly_CurrencyList []>">", quantity=2, state="active", subscription_add_ons=[], unit_amount_in_cents=1200, uuid="1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3"';
    $s = 'plan_code';
    echo value($string, $s);

这对我有用,但您也可以尝试以下功能:

$out = sscanf('Recurly_Subscription[href=https://api.recurly.com/v2/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxx] account=<Recurly_Stub[account] href=https://api.recurly.com/v2/accounts/1>, activated_at="2012-07-14 12:55:47 +00:00", currency="USD", current_period_ends_at="2012-08-14 12:55:47 +00:00", current_period_started_at="2012-07-14 12:55:47 +00:00", plan="Recurly_Plan[href=https://api.recurly.com/v2/plans/starter] name="Starter", plan_code="starter", setup_fee_in_cents="<Recurly_CurrencyList []>", unit_amount_in_cents="<Recurly_CurrencyList []>">", quantity=2, state="active", subscription_add_ons=[], unit_amount_in_cents=1200, uuid="1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3"', 'Recurly_Subscription[href=https://api.recurly.com/v2/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxx] account=<Recurly_Stub[account] href=https://api.recurly.com/v2/accounts/1>, activated_at="2012-07-14 12:55:47 +00:00", currency="USD", current_period_ends_at="2012-08-14 12:55:47 +00:00", current_period_started_at="2012-07-14 12:55:47 +00:00", plan="Recurly_Plan[href=https://api.recurly.com/v2/plans/starter] name="Starter", plan_code="%s", setup_fee_in_cents="<Recurly_CurrencyList []>", unit_amount_in_cents="<Recurly_CurrencyList []>">", quantity=2, state="active", subscription_add_ons=[], unit_amount_in_cents=1200, uuid="1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3"', $value);
echo substr($value,0,strlen($value)-2); //output: starter