user892134
user892134

Reputation: 3214

Remove brackets and letters from string, regex

I have these strings returned in variable $item_data..

180 - 190 (cm)

20 (litres)

How do i remove (cm) or (litres) from the string and only allow numbers and the dash?

$item_data = preg_replace('/[^a-zA-Z]+/', '', $item_data);

Upvotes: 0

Views: 98

Answers (2)

Saty
Saty

Reputation: 22532

you can use also

$str="180 - 190 (cm)";
echo $result = preg_replace('/[^\d-]+/', '', $str);

output:180-190

Upvotes: 1

Marc
Marc

Reputation: 3709

This should work :)

$item_data = preg_replace('/[^0-9-]+/', '', $item_data);

Upvotes: 3

Related Questions