0

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?

Matt
  • 22,721
  • 17
  • 71
  • 112
Julio María Meca Hansen
  • 1,303
  • 1
  • 17
  • 37
  • 3
    I'm not sure what you want to achieve here. Is it just reordering and replacing the dollar signs `$` with braces `{}`? What does `cr` stand for? Is this a carriage return? – Olaf Dietsche Mar 14 '13 at 11:10
  • Sorry for the delay :) Yes, exactly. **$cr** is a carriage return in this case. What I need is to translate osCommerce address format string into the kind of address format string OpenCart handles. – Julio María Meca Hansen Mar 14 '13 at 13:43
  • And the dollar signs are literal dollar signs or are they from the variable names? – Olaf Dietsche Mar 14 '13 at 14:25

2 Answers2

2

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.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
1

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
Jay Gilford
  • 15,141
  • 5
  • 37
  • 56
  • In this case, addresses are not an issue regarding ISO encoding, as osCommerce tables related to clients or invoices are empty for any address field (it's a book shop serving its clients on a local-pickup basis, so the shop is modified not to ask for address. Works more like a reservation system than a real e-shop, to be honest :P – Julio María Meca Hansen Mar 14 '13 at 13:49
  • (but I've been asked to import everything from the tables, as this is a more future-proof design, hence I need the address format string imported) :) – Julio María Meca Hansen Mar 14 '13 at 13:50
  • @JulioMecaHansen - Yes I understand, however it's required for any addresses in OC to have the ISO code for a country and region – Jay Gilford Mar 14 '13 at 14:13