dan
dan

Reputation: 729

php: extracting substring from a string

I have in my database record products and price on the same column. EG:

product name 2- RM 120.00

product name 6 - RM 10.00

product name 77 - RM 1.00

product name 3243 - RM 18.00

I am currently trying to separate the name & price into 2 columns, so the name column will only have 'product name' while price column only the price '120.00'

I tried using substr function but that would only work on the number number of characters behind. Is there anyway I can get all the numbers behind the '- RM' ?

Upvotes: 0

Views: 58

Answers (2)

gview
gview

Reputation: 15401

The explode function makes this pretty easy.

$values = explode(' - RM ', $field);
var_dump($values);

Upvotes: 0

heychez
heychez

Reputation: 648

$data = explode(" - RM ", $record); //your (string) database record in $record

echo $data[0]; //product name 2
echo $data[1]; //120.00

echo $data[2]; //product name 6
echo $data[3]; //10.00

Upvotes: 2

Related Questions