Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_match a String to get certain numbers out of the string

Tags:

regex

php

I have a string that always has the following format

Text: 1.1111111 Text

What I need is 1.11 of the string

So I went with this regex

^(\S*\s)(\d.\d{2})

I've used http://regex101.com/ to try it out and it works there, but when I do it on my own code, the matches array is always empty.

This is the code

//$ratingString = Durchschnittsbewertung: 4.65000 von 5 Sternen 20 Bewertungen Location bewerten 
preg_match ( "/^(\S*\s)(\d.\d{2})/", $ratingString, $matches );
var_dump ( $matches );
// matches == array (0) {}
like image 343
Musterknabe Avatar asked Mar 21 '23 10:03

Musterknabe


1 Answers

You need to escape the dot as dot is special character in regex which matches any character if not escaped:

^(\S*\s)(\d\.\d{2})
like image 131
anubhava Avatar answered Apr 06 '23 07:04

anubhava