编辑代码

<?php 
//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
function extractJavaScript($string) {
    // 正则表达式匹配 script 标签
    preg_match_all('/<script\b[^>]*>(.*?)<\/script>/is', $string,$matches);
    // 返回匹配到的JavaScript代码
    return $matches[1] ?? [];
}
// 示例
$htmlContent = <<<HTML
<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<script type="text/javascript">
    // some JavaScript code here
    function myFunction() {
        document.getElementById("demo").innerHTML = "Hello JavaScript!";
    }
</script>
<p id="demo">A Paragraph.</p>
</body>
</html>
HTML;
$javascriptCode = extractJavaScript($htmlContent);
print_r($javascriptCode);