Extract values from text string using PHP?

Using the Recurly API, the following text is returned. What I'm trying to figure out is the easiest way with PHP to extract values from this string and place them into PHP variables.

I've looked at various methods within the PHP manual to find text but being new to PHP the exact process to actually get the values after I've found their location still not apparent to me.

For example, how would I pull the value for plan_code and remove the quotes around the value so that the result is $plan = 'starter'?

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"

Answers


You can use Recurly's PHP API:

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

The token is a subscription object. You can then retrieve additional information about the account and its billing information like this:

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

Retrieve fields from the account (e.g. account holder first name) and its billing info (e.g. last 4 digits of credit card number of record) the same way plan_code is fetched above.

Doing the above is much easier than finding how to do so in Recurly's documentation!


A trick is turn it into a querystring syntax and use 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.

Next you use parse_str and it builds an associative array of names/values:

parse_str($newstring, $myarray);

Then you can print out and see how it looks and tweak:

print_r($myarray);

several ways to handle this.

probably the easiest one: look in the API if you can get a more standardconform result. PHP has build-in functions to handle XML, JSON and several other formats, so that you would get an array or an object on which you can access the information bits.

one possible way to do it with this format: split at the ,, split again at = to get key => value pairs. (you may need to cut the first line, though, as I don't see a , after that.) Rough sketch, untested:

<?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'];

    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);

This worked for me, but you can also try the following function:

$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

Need Your Help

Rails controller: authenticating only for certain actions

ruby-on-rails

I have a rails site with users, and these users can make certain pages.

Is scandir really thread safe?

c io thread-safety readdir scandir

In the UNIX® System Threads Reference, under the heading of "Thread-safety" is a list of functions are "not guaranteed to be thread-safe on all UNIX systems." The function scandir() is absent from ...