-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
  • 31,130
  • 7
  • 57
  • 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
  • 83,193
  • 59
  • 77
  • 109