There is such a line: "/ test 123 test 5-6 test 8 test". All tsiferki except the letters test, can vary, tell me, please, how to write them into an array, the number of spaces is the same -.-
|
2 answers
$str = "/test 123 test 5-6 test 8 test"; preg_match_all(/(test\s*[-\d]*)+/,$str,$matches); print_r($matches[0]);//[0] => Array ( [0] => test 123 [1] => test 5-6 [2] => test 8 [3] => test ) About
/(test\s*[-\d]*)+/ / и / around - limiters. They report that there is a regular inside.\s - space (and a few more characters that look like it)\s* - some spaces, 0-inf[] - character class[-\d] is a character class consisting of numbers and hyphens. The hyphen in this value is ALWAYS at the beginning.() - grouping. In general, it is not really needed here.+ - any non-zero number of times. In this case, it is also not necessary, since preg_match_all is used.
|
<?php $str = "/test 123 test 5-6 test 8 test"; $a = explode(' ', $str); ?> So not satisfied?
- I would rather do explode ('test', $ str); - knes
|