K
K
Kirill Gorelov2016-06-04 19:52:42
PHP
Kirill Gorelov, 2016-06-04 19:52:42

Parse and get word between two characters, php?

Hello.
It is necessary from the text, get the city and phone number in the form (Moscow: +71234566789).
A solution has been found.

$text = explode("Москва", $text);
$text = explode("\n", $text[1]);
echo $text[0];

How now to do that in the same way to find another city?
$text = explode("Питер: ", $text);
$text = explode("\n", $text[1]);
echo $text[0];
$text = explode("Москва: ", $text);
$text = explode("\n", $text[1]);
echo $text[0];

How to write it correctly??
Tried with regular expression
$pattern2 = "|Москва: (.+?) \n |is"; 
  preg_match($pattern2, $text, $datee); 
echo $datee;

But something didn't work!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2016-06-04
@Kirill-Gorelov

Try like this:

$s = "Москва: +71234566789   Питер: +7111111111";
preg_match_all('/(Москва|Питер):\s*(\S+)/m', $s, $r);
foreach ($r[0] as $v) {
  echo($v.'<br>');
}

A
Andrey, 2016-06-04
@GhOsT_MZ

I would do like this:

if ( preg_match( '#(.+):\s((\+)?[\s0-9\(\\)-]+)#ui', $str, $matches ) )
{
    var_dump( $matches );
}

As a result, in the array we get both the city and the phone number.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question