它';使用PHP可以从外部HTML文件中获取javascript变量
是否可以使用它';使用PHP可以从外部HTML文件中获取javascript变量,javascript,php,Javascript,Php,是否可以使用PHP从外部html文件获取Javascript变量 在这种情况下,我想获得varsrc: <script type="text/javascript"> var sources = [{ type: "video/mp4", src: "https://cdn.example.mp4", res: "Traile
PHP
从外部html
文件获取Javascript
变量
在这种情况下,我想获得var
src:
<script type="text/javascript">
var sources = [{
type: "video/mp4",
src: "https://cdn.example.mp4",
res: "Trailer",
label: "Trailer"
}];
etc.
</script>
风险值来源=[{
输入:“视频/mp4”,
src:“https://cdn.example.mp4",
res:“预告片”,
标签:“拖车”
}];
等
也许有更好的方法
但这里有一个简单的方法:
<?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, "\""));
?>
我不太理解你的问题,但希望这会有所帮助
// source.js
var mySource = {
type: "video/mp4",
src: "https://cdn.example.mp4",
res: "Trailer",
label: "Trailer"
};
在主文件中,例如index.php
<script type="text/javascript" src="source.js"></script>
然后像这样使用它:
<video width="320" height="240" autoplay>
<source src="mySource.src" type="video/mp4">
</video>
您可能需要执行一些ajax调用,然后用POST方法将数据交给php脚本。你有什么可以理解的理由不将所有js代码放在外部js文件中,然后在需要的地方将此文件加载到php中?@x-magix,因为js代码在html中file@bpy嗯,那么创建新的jsFile.js并将所有代码放在其中。然后将它加载到您需要的每个php中?你们中的大多数人做var_dump($content);