What's the problem when writing to a file in php?

There is a line:
---------------------------------------
>> Moscow: +7 (495) 565-38-71
>> Saint-Petersburg: +7 (812) 385-57-81
>> Regions: +7 (8362) 32-85-45

Shop tara-tovara.ru, info@tara-tovara.ru
Moscow: +7 (495) 565-38-71
Saint-Petersburg: +7 (812) 385-57-81
Regions: +7 (8362) 32-85-45
---------------------------------
Get a room regular expressions:
preg_match_all('/(Moscow|St. Petersburg|Regions:)\s*([\+\-\d\(\)\s]+)/m', $textt, $r);
$resultphone = array_unique($r);
$phone = array(); // array for the phone
foreach ($resultphone[0] as $v) { // iterate through phones to fill the array
$vv = trim($v);
 // echo($v.'<br>'); // for output
 $phone[] = $vv;// for recording
}
//remove duplicate
$resultphone = array_unique($phone);

The result is an array in the proper format:
Array
(
[0] => Moscow: +7 (495) 565-38-71
[1] => Saint-Petersburg: +7 (812) 385-57-81
[2] => Regions: +7 (8362) 32-85-45
)
Plucked these numbers to record
foreach($resultphone as $value) // write a good array
{
 echo $value. "\n";
 $str2.= "\n". $value; //"\n".
 }

The variable $str2 outputs in the right form:
Moscow: +7 (495) 565-38-71
Saint-Petersburg: +7 (812) 385-57-81
Regions: +7 (8362) 32-85-45
Doing a var_dump of this variable
var_dump($str2);
/home/ubuntu/workspace/gm3.php:117:
string(421) "
Array
Moscow: +7 (495) 565-38-71
Saint-Petersburg: +7 (812) 385-57-81
Regions: +7 (8362) 32-85-45
Moscow:
Saint Petersburg:
Regions:
Moscow: +7 (495) 565-38-71
Saint-Petersburg: +7 (812) 385-57-81
Regions: +7 (8362) 32-85-45
Moscow: +7 (495) 565-38-71
Saint-Petersburg: +7 (812) 385-57-81
Regions: +7 (8362) 32-85-45"

And accordingly is written to the file the same nonsense. Why is this happening?
July 8th 19 at 16:41
2 answers
July 8th 19 at 16:43
Solution
$testStr = '>> Moscow: +7 (495) 565-38-71
>> Saint-Petersburg: +7 (812) 385-57-81
>> Regions: +7 (8362) 32-85-45

Shop tara-tovara.ru, info@tara-tovara.ru
Moscow: +7 (495) 565-38-71
Saint-Petersburg: +7 (812) 385-57-81
Regions: +7 (8362) 32-85-45';

preg_match_all('/(Moscow|St. Petersburg|Regions:)\s*([\+\-\d\(\)\s]+)/m', $testStr, $resultphone);
$phone = array(); // array for the phone
foreach ($resultphone[0] as $v) { // iterate through phones to fill the array
$vv = trim($v);
// echo($v.'
'); // for output
$phone[] = $vv;// for recording
}
//remove duplicate
print_r($resultphone);
$resultphone = array_unique($phone);

$str2 = "";
foreach($resultphone as $value) // write a good array
{
// echo $value. "\n";
$str2.= "\n". $value; //"\n".
}


echo "****************\n";
echo $str2."\n";
die();
Oh, by the way, about the regular season. In original strings ":" separates the name "owner" and a phone number? If Yes, it is possible to simplify
With the code figured out, thanks.
But at the expense ranges in regexps did not understand what was meant! - Kamron29 commented on July 8th 19 at 16:46
:
Something like this:
preg_match_all("#(\w+):\s*([\+\-\d\(\)\s]+)#siu", $testStr, $resultphone);
If phones are not only "Moscow", "Saint Petersburg" and "Regions" - Nash_Marv commented on July 8th 19 at 16:49
: Ahh about it. Yeah I thought about it. I make the condition. If my condition: /(Moscow|St. Petersburg|Regions:)\s*([\+\-\d\(\)\s]+)/m Blank, search phones without a city. Thank you. - Kamron29 commented on July 8th 19 at 16:52
Not at all like - Nash_Marv commented on July 8th 19 at 16:55
July 8th 19 at 16:45
You have all the variables by different names.

preg_match writes the result to $r, and then you work at all with other names.

You do treat, get not what we need.

A code curve have posted.
Yes, jamb, corrected! - Kamron29 commented on July 8th 19 at 16:48
: created something, the code is normal. Does not have to duplicated output. Clean out all the test options, leave work. As in the question. - Nash_Marv commented on July 8th 19 at 16:51
Not all, understood. Thank you! - Kamron29 commented on July 8th 19 at 16:54

Find more questions by tags PHP