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?
Regex: ([\d\.]+)(?:mm|\")
([\d\.]+)(?:mm|\")
This will match digits , .
before mm
and "
.
<?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\.]+(?=mm|\")
match digits , .
and positively look ahead for mm
and "
<?php
ini_set('display_errors', 1);
$string='Gold 5.0mm Rolo Chain 18" In';
preg_match_all("/[\d\.]+(?=mm|\")/", $string,$matches);
print_r($matches);
want to
mm
number andIn
number from string.
I guess you can use:
preg_match_all('/[\d.]+/si', $s, $m);
print_r($m[0]);
Output:
Array
(
[0] => 5.0
[1] => 18
)
Hamlet
$string = 'Gold 5.0mm Rolo Chain 18" In"';
preg_match('/([\d\.]+)[m]{2}[A-z\ ]+([\d]+)[A-z\"\ ]+/',$string,$matches);
/**
* $match[1] - 5.0
* $match[2] - 18
*/
来源:https://stackoverflow.com/questions/43516500/get-number-before-selected-word-form-string-using-preg-match-function-in-php