0

I'm trying something like this -

$foo = "foo555bar25foobar1";
if(1 === preg_match('~[0-9]~', $foo, $matches) { 
$bar = preg_replace($matches, "-".$matches."-", $foo);
};
echo $bar;

The result I'm looking to achieve is: foo-555-bar-25-foobar-1-

TonyGonz
  • 31
  • 7

1 Answers1

2
$foo = "foo555bar25foobar1";
$bar = preg_replace('/\d+/', '-$0-', $foo);
echo $bar;
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Wow so much more simplified since I'm using array of patterns in my particular case. I am getting better at regex patterns. – TonyGonz Jan 16 '20 at 16:05