-1

What is the best way to extract the number in the following string:

"This is a test string {2461245} and I need to extract the number"

Thank you.

PioneerMan
  • 303
  • 4
  • 12

1 Answers1

1

This should help you :

$myText = 'This is a test string {2461245} and I need to extract the number';
preg_replace("/[^0-9]/","",$myText);

What you are saying there is : I want to replace everything which is not a number in the myText variable by "", which means everything which will stay in the end is your number.

Izuka
  • 2,572
  • 5
  • 29
  • 34
  • Actually I realized this won't work in all situations because the string that is being scanned may sometimes include other numbers. And what I really need is to find the number that I'm now putting inside an HTML comment, like this: . Thank you. – PioneerMan Jan 26 '16 at 01:18