首页 > 解决方案 > 可以使用 PHP 从外部 HTML 文件中获取 javascript 变量

问题描述

是否可以使用从外部文件PHP中获取变量?Javascripthtml

在这种情况下,我想得到var src:

    <script type="text/javascript">
                var sources = [{
                    type: "video/mp4",
                    src: "https://cdn.example.mp4",
                    res: "Trailer",
                    label: "Trailer"
                }];
                etc.
    </script>

标签: javascriptphp

解决方案


也许有更好的方法。

但这里有一个简单的方法:

<?php
$content = file_get_contents("path to javascript");

//Remove everything before the string
$src = substr($content, strpos($content, "src: \"")+6, 100);

//Remove everything after the string
$src = substr($src, 0, strpos($src, "\""));
?>

推荐阅读