O
O
odd-look2014-11-16 16:05:47
PHP
odd-look, 2014-11-16 16:05:47

How to replace a piece of a string with another piece?

Actually the problem is to replace the city identifier (number) in api vk with a normal name.
There is a line like:
"name": Vasya
"birthday": 1/1/1990
"city": 1
And a line like this:
"name": Moscow
So, I need to swap "1" and "Moscow" and output one normal line, to make it clear what kind of city.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-11-16
@odd-look

$cities = json_decode($citiesVk, true);
$citiesNamesMap = array_column($cities, 'name', 'id');

$user = json_decode($vkUser, true);
$user['city'] = $citiesNamesMap($user['city']);

somehow so visible. And here the lines ...
But if you specifically answer your question - regular expressions will cope with this. Specifically preg_replace_callback

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question