If you try to parse JS code in PHP
$subject = <<<'__EOS__'
var companies = [{"name":"Beliaa","lat":"30.043438999999999","lng":"31.239159999999998","infowindow":{"title":"Beliaa","address":"28 El Falaky St., Bab El Louk, Downtown, Cairo"}}];
translator.add("Rated Successfully", "Rated Successfully");
translator.add("Reviewed Successfully", "Your review has been submitted and will be published as soon as possible. Thanks for sharing!");
__EOS__;
if(preg_match('/"lat":"(.*?)"/', $subject, $matches))
echo "lat:{$matches[1]}";
else
echo 'not found';
Since your source string is a very simple snippet, you can keep your regular expression simple as well. The searched string is surrounded by the literal "lat":"
..."
. The parenthesis define a subpattern (.*?)
which is captured in $matches[1]
. The dot means "any character", the asterisk is a quantifier meaning 0 or more times, the questionmark makes the quantified expression ungreedy, so it stops on the next matched pattern behind this expression (the quotes in this case). Otherwise you would get the entire string until sharing!"
(the last quotes found).