There is text in the $ a variable, you need to cut out the phone number from the whole text (here is the regular / 89 \ d {9} /) and insert it into the $ b variable. Tell me please.

  • publish an example of the original string. Number in the line can occur only once? - Edward
  • Example: 10 meters of glowing in the dark plastic and a set of stencils as a gift. A trial plastic kit is included in the kit with a 3D pen, but it is not enough to create independent toys or compositions. For a simple figure, you will need from 10 meters of plastic, and for more complex figures you may need up to 100 meters. Tel89999999999 - Dmitriy
  • Yes, the number occurs only once. - Dmitriy

1 answer 1

If the number occurs only once, then you can do with the preg_replace() function. Create a pattern so that it matches the entire line, and its submask refers to a number of 11 digits, starting with 89:

 $str = 'Пример: 10 метров светящегося в темноте пластика и набор трафаретов в Подарок. В комплект с 3D ручкой входит пробный набор пластика, но для создания самостоятельных игрушек или композиций его недостаточно. Для простой фигурки понадобится от 10 метров пластика, а для более сложных фигур может понадобится до 100 метров. тел89999999999'; echo preg_replace('~.*(\b89\d{9}\b).*~', '$1', $str); 

Result:

 89999999999 

UPD You can otherwise solve this example with preg_match() . In this case, it is necessary to look for matches only by number, and accordingly references to submasks are not required here:

 $str = 'Пример: 10 метров светящегося в темноте пластика и набор трафаретов в Подарок. В комплект с 3D ручкой входит пробный набор пластика, но для создания самостоятельных игрушек или композиций его недостаточно. Для простой фигурки понадобится от 10 метров пластика, а для более сложных фигур может понадобится до 100 метров. тел89999999999'; preg_match('~\b89\d{9}\b~', $str, $arr); $number = $arr[0] ?? false; echo $number;