使用PHP检索head标记中多个脚本标记的属性和内容
我发现了一些与我的问题相关的不同问题,但我很难将它们组合成一个函数 这是我的HTML:使用PHP检索head标记中多个脚本标记的属性和内容,php,Php,我发现了一些与我的问题相关的不同问题,但我很难将它们组合成一个函数 这是我的HTML: <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>microscope</title> <script language="javascript">AC_FL_RunContent = 0;</script> &
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>microscope</title>
<script language="javascript">AC_FL_RunContent = 0;</script>
<script src="Scripts/AC_RunActiveContent.js" language="javascript"></script>
</head>
显微镜
AC_FL_RunContent=0;
以下是我现在掌握的代码:
$filePath = "directory/file.html";
retrieveScriptContentandAttributes($filePath);
function retrieveScriptContentandAttributes($filePath) {
$dom = new DOMDocument;
@$dom->loadHTMLFile($filePath);
//var_dump($dom->loadHTMLFile($filePath));
$head = $dom->getElementsByTagName('head')->item(0);
$xp = new DOMXpath($dom);
$script = $xp->query("script", $head);
for ($row = 0; $row < 5; $row++) {
echo $script->item($row)->textContent;
if ($script->item($row) instanceof DOMNode) {
if ($script->item($row)->hasAttributes()) {
foreach ($script->item($row)->attributes as $attr) {
$name = $attr->nodeName;
$value = $attr->nodeValue;
$scriptAttr[] = array('attr'=>$name, 'value'=>$value);
}
echo $scriptAttr;
}
}
}
$filePath=“directory/file.html”;
检索脚本内容和属性($filePath);
函数检索ScriptContentAndAttribute($filePath){
$dom=新的DOMDocument;
@$dom->loadHTMLFile($filePath);
//var_dump($dom->loadHTMLFile($filePath));
$head=$dom->getElementsByTagName('head')->项(0);
$xp=新的DOMXpath($dom);
$script=$xp->query(“脚本”,$head);
对于($row=0;$row<5;$row++){
echo$script->item($row)->textContent;
如果($script->item($row)instanceof DOMNode){
如果($script->item($row)->hasAttributes()){
foreach($script->item($row)->属性为$attr){
$name=$attr->nodeName;
$value=$attr->nodeValue;
$scriptAttr[]=array('attr'=>$name,'value'=>$value);
}
echo$scriptAttr;
}
}
}
我得到的结果是“ArrayAC\u FL\u RunContent=0;数组注意:尝试获取非对象的属性”,在“echo$script->item($row)->textContent。奇怪的是,这行执行得很好。但是我需要一种方法来获取$scriptAttr以打印数组,如下所示:language=>javascript。然后对于下一个脚本标记:src=>Scripts/AC_RunActiveContent.js,language=>javascript
非常感谢您的帮助!!尝试DOMXpath(请参阅:):
您可以在消除getElementsByTagName调用的同时清理代码:
$dom = new DOMDocument;
@$dom->loadHTMLFile($filePath);
$xp = new DOMXpath($dom);
$scripts = $xp->query("//head/script"); // find only script tags in the head block, ignoring scripts elsewhere
foreach($scripts as $script) {
.... your stuff here ...
}
xpath查询返回的DOMNoteList是可编辑的,因此您可以简单地对其进行foreach,而无需进行计数/for循环。通过直接xpath查询执行此操作,您不必检查
$script
节点是否为脚本节点……这是查询结果将返回的唯一节点类型。所以您只需要所有属性对于数组中的每个脚本标记?如果使用var_dump$script->item($row)会发生什么情况
?是的,我需要所有属性和属性的内容。输出:ArrayAC_FL_RunContent=0;数组更接近,谢谢!当我将查询更改为//head/script时,这非常接近。我如何访问/搜索数组的各个成员?例如,如果我只需要第二个数组的源属性的值?@EllaJo Jus不要像其他数组一样访问它,例如:echo$scriptAttributes[1]['src']
(记住数字数组索引从0开始)。您还可以将xpath查询(//head/script
)更改为仅选择实际具有src属性的脚本节点。facepalm非常明显。谢谢Yoshi。$scriptAttrs=array();$scriptAttrs=retrieveScriptContentandAttributes($filePath);for($row=0;$row)
array(2) {
[0]=>
array(1) {
["language"]=>
string(10) "javascript"
}
[1]=>
array(2) {
["src"]=>
string(30) "Scripts/AC_RunActiveContent.js"
["language"]=>
string(10) "javascript"
}
}
$dom = new DOMDocument;
@$dom->loadHTMLFile($filePath);
$xp = new DOMXpath($dom);
$scripts = $xp->query("//head/script"); // find only script tags in the head block, ignoring scripts elsewhere
foreach($scripts as $script) {
.... your stuff here ...
}