php谷歌地图正则表达式preg_match

i have this data

  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!");

i want to get

lat:30.043438999999999 and lng:31.239159999999998

with preg_match

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).

Quasimodo's answer will work, but it can be better. The following method uses a faster regex pattern and eliminates (waste) the need to use a capture group.

One-liner Method:

list($lat,$lng)=preg_match_all('/"l(?:at|ng)":"\K[\d\.]+/',$in,$out)?$out[0]:['',''];
echo "lat:$lat<br>";
echo "lng:$lng";

Output:

lat:30.043438999999999
lng:31.239159999999998

The \K in the regex pattern will store your desired values in the "fullstring" subarray. This technique reduces array bloat by 50%. This part: l(?:at|ng) will efficiently match lat and lng. And this: [\d\.]+ will match the entire decimal substring that you require.

list() will assign the matched values to ready-to-use variables; now you can use these variables as needed.