A
A
Alexander Shapoval2018-02-03 19:22:41
PHP
Alexander Shapoval, 2018-02-03 19:22:41

How to substitute values ​​in a pattern?

Good day, help solve the problem.
There is a string (uk|en|ru|ko)/chat_mini/actions/([0-9]+)/([az]{,9})
And an array
['ru', 9, 'test']
how to get ru /chat_mini/actions/9/test ?
Decision:

$pattern = '(uk|en|ru|ko)/chat_mini/actions/([0-9]+)/([a-z]{,9})';
preg_match_all('/(\([^\(]*)\)/i', $pattern, $matches); // собираем всё что в скобках для последующей замены
$arr = ['ru', 89, 'test']; // массив значений которые нужно будет подставить
$string = str_replace($matches[0], $arr, $pattern); // наша строчка с вставленными параметрами
echo 'Исходная строчка:<br>';
echo $pattern . '<br>';
echo 'Результат:<br>';
echo $string;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman PHP, 2018-02-03
@sanek_os9

back - str_replace

менять массив ['(uk|en|ru|ko)', '([0-9]+)', '([a-z]{,9})'] на массив ['ru', '9', 'test']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question