A
A
Anton2017-08-12 16:20:36
PHP
Anton, 2017-08-12 16:20:36

How to make regex work in php?

Hello. There is such a regular expression "(\s|^)(\[.*?\]|.*?)(?=(\s|$))" in js when I run it like this, the line is split into an array

'Купить !автомобиль [недорого москва] [новый или !бу]'.match(/(\s|^)(\[.*?\]|.*?)(?=(\s|$))/g).map(x=>x.trim())
but in php it doesn't work like that, it returns 5 empty array elements.
preg_split('/(\s|^)(\[.*?\]|.*?)(?=(\s|$))/', 'Купить !автомобиль [недорого москва] [новый или !бу]')
Why is this and how can it be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav B, 2017-08-12
Reytarovsky @Antonchik

preg_match_all

preg_match_all('/(\s|^)(\[.*?\]|.*?)(?=(\s|$))/', 'Купить !автомобиль [недорого москва] [новый или !бу]', $m);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question