1

I have following value in a table column is there any option to get the sandbox value and merchant_email like array ? I know using explode and loop is works but any other smart options ? something like $result->sandbox

 paypal_merchant_email="[email protected]"|paypal_verified_only="0"|payment_currency=""|sandbox="1"|sandbox_merchant_email="[email protected]"|payment_logos=""|debug="0"|status_pending="W"|status_success="O"|status_canceled="D"|countries=""|min_amount="0"|max_amount="0"|secure_post=""|ipn_test=""|no_shipping="0"|address_override="0"|cost_per_transaction="0"|cost_percent_total="0"|tax_id=0|

Thanks in advance!

5
  • 2
    Is something like stackoverflow.com/questions/15966693/… you mean? Commented Sep 24, 2013 at 3:35
  • I think the OP knows how to do it with explode etc, but is asking if there's a better way given that string format. I think exploding twice is perfectly good enough... Commented Sep 24, 2013 at 3:40
  • Why dont you take the response as JSON and simple use json_decode($result->sandbox, TRUE);? Also it would help to see what you're currently using. Commented Sep 24, 2013 at 3:41
  • @Prix its not comes from paypal the Virtuemart is saving payment gateway details in this format in DB Commented Sep 24, 2013 at 3:44
  • @JobinJose I have also added a method using preg_match_all to the benchmark which is in question of writing probably the simplest one. Commented Sep 24, 2013 at 4:12

6 Answers 6

1

Little benchmark magic:

<?php
// explode
$start = microtime(TRUE);
$data = array();
foreach (explode('|', $result->sandbox) as $item)
{
    if (empty($item)) continue;
    list($key, $value) = explode("=", $item);
    $data[$key] = str_replace('"', '', $value);
}
print_r($data);
$stop = microtime(TRUE);
$timeResult = $stop - $start;
echo $timeResult, "\n";

//preg_split
$start = microtime(TRUE);
$data = array();
foreach (preg_split('/\|/', $result->sandbox) as $item)
{
    if (empty($item)) continue;
    list($key, $value) = preg_split('/=/', $item);
    $data[$key] = str_replace('"', '', $value);
}
print_r($data);
$stop = microtime(TRUE);
$timeResult = $stop - $start;
echo $timeResult, "\n";

//preg_match_all
$start = microtime(TRUE);
$data = array();
preg_match_all('/([^=]+)="([^"]+)?"\|/', $result->sandbox, $result);
$data = array_combine($result[1], $result[2]);
print_r($data);
$stop = microtime(TRUE);
$timeResult = $stop - $start;
echo $timeResult, "\n";

Result:

Array
(
    [paypal_merchant_email] => [email protected]
    [paypal_verified_only] => 0
    [payment_currency] =>
    [sandbox] => 1
    [sandbox_merchant_email] => [email protected]
    [payment_logos] =>
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] =>
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] =>
    [ipn_test] =>
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
)
0.00029397010803223

Array
(
    [paypal_merchant_email] => [email protected]
    [paypal_verified_only] => 0
    [payment_currency] =>
    [sandbox] => 1
    [sandbox_merchant_email] => [email protected]
    [payment_logos] =>
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] =>
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] =>
    [ipn_test] =>
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
)
0.00031495094299316

Another:

Array
(
    [paypal_merchant_email] => [email protected]
    [paypal_verified_only] => 0
    [payment_currency] =>
    [sandbox] => 1
    [sandbox_merchant_email] => [email protected]
    [payment_logos] =>
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] =>
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] =>
    [ipn_test] =>
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
)
0.00026917457580566

Array
(
    [paypal_merchant_email] => [email protected]
    [paypal_verified_only] => 0
    [payment_currency] =>
    [sandbox] => 1
    [sandbox_merchant_email] => [email protected]
    [payment_logos] =>
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] =>
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] =>
    [ipn_test] =>
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
)
0.00028419494628906
Sign up to request clarification or add additional context in comments.

6 Comments

What is the significance of your benchmark? It is pointless, and you are saving microseconds.
@RPM the significance is to show him that he could have done that from the start to see which method would perform the best to what he needs done but feel free to get mad at it.
@RPM Oh I am sorry that I actually have 3 different methods with a benchmark on it.
@JobinJose it seems the preg_match_all performs the best over 1k iterations.
@Prix The question wasn't about benchmarking. It was about parsing a string.
|
1
$string = 'paypal_merchant_email="[email protected]"|paypal_verified_only="0"|payment_currency=""|sandbox="1"|sandbox_merchant_email="[email protected]"|payment_logos=""|debug="0"|status_pending="W"|status_success="O"|status_canceled="D"|countries=""|min_amount="0"|max_amount="0"|secure_post=""|ipn_test=""|no_shipping="0"|address_override="0"|cost_per_transaction="0"|cost_percent_total="0"|tax_id=0|';

$result = array();
$string = preg_split('/\|/', $string);
foreach($string as $key => $value) 
{
    $value = str_replace('"', '', $value);
    $value = preg_split('/=/', $value);
    if(strlen($value[0])> 0) 
    {
       $result[$value[0]] = array_key_exists(1, $value) ? $value[1] : NULL;
    }
}

echo "<pre>";
print_r($result);
echo "</pre>";

Output

Array
(
    [paypal_merchant_email] => [email protected]
    [paypal_verified_only] => 0
    [payment_currency] => 
    [sandbox] => 1
    [sandbox_merchant_email] => [email protected]
    [payment_logos] => 
    [debug] => 0
    [status_pending] => W
    [status_success] => O
    [status_canceled] => D
    [countries] => 
    [min_amount] => 0
    [max_amount] => 0
    [secure_post] => 
    [ipn_test] => 
    [no_shipping] => 0
    [address_override] => 0
    [cost_per_transaction] => 0
    [cost_percent_total] => 0
    [tax_id] => 0
    [] => 
)

11 Comments

thanks for your answer. that same as explode and loop iam looking for better option bcoz that looping is take time if the string length high
See my edit. It is easier to just transform the whole thing into an associative array.
Thanks for your time +1 efforts
How is this different from normal explode() ? You are using simple regex that can be done easily with explode().
@AyeshK would recommend you do some benchmark on that as it may be heavier than using explode, however you would only know that by actually doing it and the different from both would be pretty insignificant.
|
0

There is a function parse_str() but it is for & delimiter: http://php.net/manual/en/function.parse-str.php

Or you can impress somebody with knowledge or PHP functions:

$str = 'a=13|b="string"|';

$rawarray = preg_split('/\|/', $str, -1, PREG_SPLIT_NO_EMPTY);
$keys = array_map(create_function('$a', '$r = preg_split("/=/", $a); return $r[0];'), $rawarray);
$values = array_map(create_function('$a', '$r = preg_split("/=/", $a, 2); return eval("return $r[1];");'), $rawarray);
$result = array_combine($keys, $values);

print_r($result);

Comments

0

If you need just one or two values, use regular expressions:

function get_value_from_string($key, $str) {
    return preg_match('/\|'.$key.'=([^\|]*)/', '|'.$str, $matches) ? eval("return $matches[1];") : null;
}

1 Comment

Thanks for your answer, No friend I need almost all the values.
0

Here's possibly a more elegant solution using str_getcsv, array_walk, use, parse_str and the venerable each (to extract the key / value pairs). With your data:

$string = 'paypal_merchant_email="[email protected]"|paypal_verified_only="0"|payment_currency=""|sandbox="1"|sandbox_merchant_email="[email protected]"|payment_logos=""|debug="0"|status_pending="W"|status_success="O"|status_canceled="D"|countries=""|min_amount="0"|max_amount="0"|secure_post=""|ipn_test=""|no_shipping="0"|address_override="0"|cost_per_transaction="0"|cost_percent_total="0"|tax_id=0|';

Idea is to first use str_getcsv to parse the |-separated line, then traverse it using fast PHP functions.

$t = str_getcsv( $string, "|" );

At this point, $t is a simple array containing broken up "key=value" pairs.

$new = array();
array_walk( $t, function( &$a ) use (&$new) {
    parse_str( $a, $b);            # parses each line, returns key-value
    $c = each( $b );               # need to obtain key-value from above
    $new[$c["key"]] = $c["value"]; # simple assignment
});

This requires PHP > 5.3.0 since it uses namespaces (use) in the anonymous function. However, if you made it this far, print_r( $new ); gives you this:

Array (
    [paypal_merchant_email] => "[email protected]"
    [paypal_verified_only] => "0"
    [payment_currency] => ""
    ...

Comments

0

Here is the very compact solution for you:

preg_match_all('/\|(.*?)="(.*?)"/', '|'.$string, $matches);
$result = array_combine($matches[1], $matches[2]);

Test it here: https://3v4l.org/rfBuc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.