skip to Main Content

I need to import an entire e-shop made with osCommerce into OpenCart. So far, so good. I have successfully imported almost everything, except for SEO and some sparse data bits, but I have a problem with the address format.

osCommerce uses $firstname $lastname$cr$streets$cr$city, $postcode$cr$statecomma$country while OpenCart uses {company} {firstname} {lastname} {address_1} {address_2} {postcode} {city} {country}.

Maybe I’m wrong, but taking a look at the basic differences between those string sets, I thought a regex would be the appropiate tool to convert an osCommerce-formatted address format string into an OpenCart-formatted address format string.

I’m, however, completely null at regexes. Can anyone tell me which regex would fit best my needs? Or, in case it’s doable without regexes or they’re a bad idea, which method should I try with them?

2

Answers


  1. I assume cr stands for a carriage return or a newline and there are literal dollar signs $ in the source string.

    For reordering you must capture the parts of the address and use them in the replacement string

    ^$(.+?)$(.+?)$.$(.+?)$.$(.+?)$(.+?)$.$(.+?)$(.+?)$(.+)$
    

    and then replace it with

    {}{$1}{$2}{$3}{}{$5}{$4}{$7}
    

    So this would become

    $re = '/^\$(.+?)\$(.+?)\$.\$(.+?)\$.\$(.+?)\$(.+?)\$.\$(.+?)\$(.+?)\$(.+)$/s';
    $replacement = '{}{$1}{$2}{$3}{}{$5}{$4}{$7}';
    $new_address = preg_replace($re, $replacement, $address);
    

    But you can also use explode for splitting

    $parts = explode('$', $address);
    

    and then put the parts together again by reordering and inserting the braces.

    Login or Signup to reply.
  2. OpenCart addresses are stored as separate fields for each of those pieces of data. The format for OC that you’ve given is merely for visual data like the address on an invoice. All data for that address however has it’s own field which you can see if you take a look at your address or order tables. It’s worth noting that addresses have ISO codes attached to them too so you will need to take that into consideration when importing to avoid issues. Assuming osCommerce stores the data in a similar fashion (I’ve not used it personally) then you would simply need to map the old columns to the new one. If they are however stored as a single piece of text and you need to extract it, you can use this

    %^(w+) (w+)s+([w ]+)s+([w ]+)s+([w ]+)s+([w]+),s*(w+)$%
    

    Tested using this as an example address

    My Name
    Street name
    City name
    P05T C0D3
    State, Country
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search