Answer the question
In order to leave comments, you need to log in
How to find a word in the page text and replace it with a word from an array?
Hello!
Please help with the problem: There is
a
list of cities on the page - there are about 400 of them, but for example I will give a few of them:
Astrakhan
Achinsk
Balakovo
Balakhna
Balashikha
Balashov
Barnaul
Bataysk
Belgorod
there is an array with a list of the same cities, but in the prepositional case, like this one for example:
$array_from_to = array (
'Узловая' => 'Узловой',
'Ростов-на-Дону' => 'Ростове-на-Дону',
'Астрахань' => 'Астрахани',
'Ачинск' => 'Ачинске',
'Балаково' => 'Балаково',
'Балахна' => 'Балахне',
'Балашиха' => 'Балашихе',
'Балашов' => 'Балашове',
'Барнаул' => 'Барнауле',
'Батайск' => 'Батайске',
'Белгород' => 'Белгороде'
);
Answer the question
In order to leave comments, you need to log in
Try like this
$text = str_replace(array_keys($array_from_to), array_values($array_from_to), $text);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question