Aryan G
Aryan G

Reputation: 1301

How to get text from preg_match()?

$text = "About 5,590 results";

preg_match("/of about <b>(.*?)</", $text, $matches);

it return the 5590 but when I tried with :

$text = "4 results";
preg_match("/of about <b>(.*?)</", $text, $matches);

is there any pattern for this case? I want to get "4" from "4 results"

Upvotes: 0

Views: 136

Answers (1)

Aurelio De Rosa
Aurelio De Rosa

Reputation: 22172

If you just need the numbers, you can use this regex:

$regex = "/\d+[\.,]?\d*/";

This will match numbers in the following formats:

  • 5
  • 5.590
  • 5,590
  • 5590

Upvotes: 5

Related Questions