Get number before selected word form string using preg_match function in php
问题 I am trying to get the mm and inches values from the following string using preg_match . $string = "Gold 5.0mm Rolo Chain 18" In"; How can I do this? 回答1: Regex: ([\d\.]+)(?:mm|\") ([\d\.]+)(?:mm|\") This will match digits , . before mm and " . PHP code demo <?php ini_set('display_errors', 1); $string='Gold 5.0mm Rolo Chain 18" In'; preg_match_all("/([\d\.]+)(?:mm|\")/", $string,$matches); print_r($matches);// its 1 index will return your desired result Or: Regex: /[\d\.]+(?=mm|\")/ [\d\.]+(?