Preg_match a String to get specific numbers from a string

I have a line that always has the following format

Text: 1.1111111 Text

I need a row 1.11string

So I went with this regex

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

I used http://regex101.com/ to try it and it works there, but when I do it in my code, the matching 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) {}
+4
source share
2 answers

You need to avoid the dot, since the dot is a special character in the regular expression that matches any character if not escaped :

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

god, so ... sorry for editing. like this

$ratingString = "Durchschnittsbewertung: 4.65000 von 5 Sternen 20 Bewertungen Location bewerten";
preg_match ( "#(\d\.\d{2})#", $ratingString, $matches );
var_dump ( $matches );
+2
source

All Articles