Search code examples
phppreg-match

preg_match selecting $, meter?


Actually I have this data

Price $ 1,600,000 Land Size 16m x 27m 

I want to get this

$1,600,000

16m x 27m

I tried this

/Land Size(.\d+m.x.\d+m)/

/Price(.\$.\d.\d+,.\d+)/

I got the result as I want, but I wonder what is the result if m change to km and $ 1,600,000 change to $1600000 or 1600000$. Is there any easy way to solve this? I hope everyone will help me. Thank in advanced


Solution

  • You can use the question mark for characters that will not always be there. However, I would recommend the following regexes:

    This will match sizes in meters and kilometers.

    /Land Size (\d+k?m x \d+k?m)/
    

    This will match a dolar sign followed by a combination of decimals and comma's.

    /Price (\$ [\d,]+)/