I have the following strings I need to extract only the countries
Roaming atv te da la bienvenida a Peru.Llamadas
Roaming atv te da la bienvenida a Reino Unido.Llamada
Roaming atv te da la bienvenida a Espana.Llamada
Roaming atv te da la bienvenida a Guadeloupe.Llamada
N times countries can repeat the same countries
I have tried to test with the following code:
$nombre = "Roaming atv te da la bienvenida a Peru.Llamadas";
$arrayNombre = explode(" ", $nombre, 7);
print_r($arrayNombre[6]);
The result is as follows:
to Peru. Calls
I need to get the country out of that chain
I recommend you use
strrpos()
php as this will return the position you need to split the string by and then usesubstr()
to split the phrase.Here's an example:
The correct and scalable way is to use a regular expression to extract the full country name between the strings start [" to "] and end [".Call"]. It works for all examples.
Official preg_match documentation on PHP.Net
What I would do is make a
explode
by spaces of the text string, where I would divide the string and save it in aarray
.The last element would be
Peru.Llamadas
. To pick it up fromarray
just get the last item:And do a
explode
by point again: