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
您確定這是返回的*確切*文本嗎? [文檔](http://docs.recurly.com/api/subscriptions)說它返回XML。 – 2012-07-17 15:25:10