Imrul.H
Imrul.H

Reputation: 5870

Explode string on hyphens surrounded by spaces

I have a string which can be one of the following:

$code = "123456 - US";
$code = "12-3456 - US";
$code = "WD123 - US";
$code = "A0-12 - US";
$code = "123456 - Poland";

How can I find out the first part (like 123456 or A0-12) and the last part (like-US or Poland)?

Upvotes: 2

Views: 96

Answers (1)

Bob Baddeley
Bob Baddeley

Reputation: 2262

you could use explode to split it into two parts.

$split = explode(" - ",$code);

That will give you an array with two elements: "123456" and "US"

Upvotes: 7

Related Questions