
<?php
const KEY_INDEX = 1;
$str = <<< EOF
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-analytics.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "xxxxxxxxxxxxx",
authDomain: "xxxxxxxxx",
databaseURL: "https://example.com",
projectId: "example-dev",
storageBucket: "sample-dev.appspot.com",
messagingSenderId: "xxxxxxxxxxxx",
appId: "1:xxxxxxxx:web:xxxxxxxxxxxxx",
measurementId: "G-xxxxxxxxxxxx"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
EOF;
$firebase_config = [];
$match_api_key = [];
$match_project_id = [];
$match_app_id = [];
$match_measurement_id = [];
$replace = [
// '置換前の文字' => '置換後の文字',
'"' => '',
'\'' => '',
'<' => '',
'>' => '',
'&' => '',
];
preg_match('/apiKey: "(.+)"/', $str, $match_api_key);
preg_match('/projectId: "(.+)"/', $str, $match_project_id);
preg_match('/appId: "(.+)"/', $str, $match_app_id);
preg_match('/measurementId: "(.+)"/', $str, $match_measurement_id);
$api_key = str_replace(array_keys($replace), array_values($replace), $match_api_key[KEY_INDEX]);
$project_id = str_replace(array_keys($replace), array_values($replace), $match_project_id[KEY_INDEX]);
$app_id = str_replace(array_keys($replace), array_values($replace), $match_app_id[KEY_INDEX]);
$measurement_id = str_replace(array_keys($replace), array_values($replace), $match_measurement_id[KEY_INDEX]);
$firebase_config = [
'api_key' => $api_key,
'project_id' => $project_id,
'app_id' => $app_id,
'measurement_id' => $measurement_id,
];
unset($match_api_key);
unset($match_project_id);
unset($match_app_id);
unset($match_measurement_id);
var_dump($firebase_config);
$ php parse_test.php
array(4) {
["api_key"]=>
string(39) "xxxxxxxxxxxxxxx"
["project_id"]=>
string(11) "sample-dev"
["app_id"]=>
string(41) "1:xxxxxxxxx:web:xxxxxxxxxxxxx"
["measurement_id"]=>
string(12) "G-xxxxxxxxxxxx"
}
関連記事 - More from my site -