B
B
bullet20182015-12-24 10:02:38
PHP
bullet2018, 2015-12-24 10:02:38

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

3 answer(s)
P
Pavel Belyaev, 2015-12-24
@bullet2018

$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);

E
Evgeny Neverov, 2015-12-24
@deMone

preg_match_all('#[0-9,\.]+#i', '5,5 x 15 ET45', $m);
print_r($m);

A
Artem Rogov, 2016-04-24
@artemyrogow

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 question

Ask a Question

731 491 924 answers to any question