Answer the question
In order to leave comments, you need to log in
How to get an array from a string?
Hello. I can't figure out how to get an array from strings for:
$str = '175/60 R15';
need an array
Array
(
[0] => 175
[1] => 60
[2] => R15
)
And also for such a string (the first number can be both integer and fractional):
$str2='5,5 x 15 ET45 ';
array:
Array
(
[0] => 5.5
[1] => 15
[2] => 45
)
Answer the question
In order to leave comments, you need to log in
$str = '175/60 R15';
preg_match('!^([0-9]+)\/([0-9]+)\s(.*?)$!siu', $str,$res);
print_r($res);
$str='5,5 x 15 ET45';
preg_match('!^([0-9,.]+)\sx\s([0-9,.]+)\s(.*?)$!siu', $str,$res);
print_r($res);
preg_match_all('#[0-9,\.]+#i', '5,5 x 15 ET45', $m);
print_r($m);
Not a lot of clumsy way, but still possible:
$str = '175/60 R15';
$str2='5,5 x 15 ET45';
function myfunct($sym = [],$str = ''){
$editString = str_replace($sym, $sym[0],$str);
$result = explode($sym[0],$editString);
return $result;
}
$result2 = myfunct(array("/"," "),$str);
$result3 = myfunct(array("x ", " ET"),$str2);
var_dump($result2);
var_dump($result3);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question