-1

Alright here is what I have

$string=Something something 1234-123'; //can be an arbitrary amount of digits on each side of hyphen $numbers=preg_replace("/.*([0-9]*-[0-9]*).*/","$1", $string); echo $numbers;

This is printing out just "-123", it should be printing out 1234-123. I feel like i'm just missing something really simple here.

2 Answers2

1

Should be more non-greedy I guess

.*?([0-9]*-[0-9]*).*

Community
  • 1
  • 1
Dhiraj
  • 32,144
  • 8
  • 59
  • 77
0

I'd use preg_match instead:

$string = 'Something something 1234-123';
preg_match('/\d+-\d+/', $string, $match);
echo $match[0],"\n";

Output:

1234-123
Toto
  • 86,179
  • 61
  • 85
  • 118