- preg_replace_callback: documentation ( source)
<?php
$msg = 'grjqgbqgrqg0 38975893240- http://www.gg.pl/afa31f312u34b151j5b1598139189 245673256786746545678987654';
$adNr = preg_replace_callback('#http://www.gg.pl/[^\s]+#', function($ma) {}, $msg);
echo $adNr;