0

I'd like to export the woocommerce array "redq_day_ranges_cost" into XML format with WP All Export. If I try to unserialize the array I run into this PHP Error: unserialize(): Error at offset...

I tried the things mentioned here: How to repair a serialized string which has been corrupted by an incorrect byte count length?

The problem is, that WP All Export already fails on saving an export template due to that error, so I feel a little lost here.

This is the data to be exported:

a:7:{
i:0;a:4
{s:8:"min_days";s:1:"1";s:8:"max_days";s:1:"1";s:10:"range_cost";s:2:"340";s:15:"cost_applicable";s:5:"fixed";}
i:1;a:4:
{s:8:"min_days";s:1:"2";s:8:"max_days";s:1:"2";s:10:"range_cost";s:2:"450";s:15:"cost_applicable";s:5:"fixed";}
i:2;a:4:
{s:8:"min_days";s:1:"3";s:8:"max_days";s:1:"3";s:10:"range_cost";s:2:"570";s:15:"cost_applicable";s:5:"fixed";}}

the plan is to export it something like

<min_days>1</min_days>
<max_days>1</max_days>
<range_cost>340</range_cost>
<cost_applicable>fixed</cost_applicable>

So far, if I call unserialize in the function editor the result is the named error. If I call it inline within the XML Editor it results in 3 rows of

<redq_day_ranges_cost>Array</redq_day_ranges_cost>

1 Answer 1

1

You have two issues with your data, the first is that it has newlines in it which need to be removed, and the second is that some of your string variables have mismatched lengths. The former can be corrected with a simple str_replace; the latter can be corrected using the code in this answer. This code will work to convert your string back to an array:

$data = str_replace("\n", "", $data);
$fixed_data = preg_replace_callback ( '!s:(\d+):"(.*?)";!', function($match) {      
    return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
}, $data );

print_r(unserialize($fixed_data));

Output:

Array ( 
  [0] => Array (
    [min_days] => 1
    [max_days] => 1
    [range_cost] => 340
    [cost_applicable] => fixed
  )
  [1] => Array (
    [min_days] => 2
    [max_days] => 2
    [range_cost] => 450
    [cost_applicable] => fixed
  )
  [2] => Array (
    [min_days] => 3
    [max_days] => 3
    [range_cost] => 570
    [cost_applicable] => fixed
  )
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

5 Comments

I added the \n just to make it more readable in here. About the mismatch, I fixed that, thank you for mentioning. The problem is, that the function editor of WP All Import still fails on saving the "export template", so I suppose there must be something else wrong with the data.
@achiever ah, sorry, I haven't helped at all. I would recommend adding the wordpress tag to your question, hopefully it might get some attention from people with more knowledge in that area. But, can you not add the call to preg_replace_callback inside the call to unserialize in the function editor e.g. unserialize(preg_replace_callback(...))?
Na, it was just what I needed to figure out. This is not a problem in the code or with the data, it just throws an error on saving the XML template. The function itself runs properly and I can get the exported data now. Thank you :)
@achiever as long as you're happy. there's no reason to accept an answer if it didn't actually solve the problem in your question.
I had the mismatched lengths in the represantation, so you solved my problem. ;)

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.