A
A
Alex-Broudy2019-01-17 20:53:20
PHP
Alex-Broudy, 2019-01-17 20:53:20

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 (
  'Узловая' => 'Узловой',
  'Ростов-на-Дону' => 'Ростове-на-Дону',
        'Астрахань' => 'Астрахани',
        'Ачинск' => 'Ачинске',
        'Балаково' => 'Балаково',
        'Балахна' => 'Балахне',
        'Балашиха' => 'Балашихе',
        'Балашов' => 'Балашове',
        'Барнаул' => 'Барнауле',
        'Батайск' => 'Батайске',
        'Белгород' => 'Белгороде'
);

and as a result, it is necessary that the cities in the list on the page, those that match the cities in this array, are replaced.
It is clear that this is done through str_replace , but I practically do not understand this, although I am trying to understand the principle.
I would be very grateful to some more or less ready-made solution and help from your side!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Georgy Baruchyan, 2019-01-18
@Alex-Broudy

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 question

Ask a Question

731 491 924 answers to any question