简体   繁体   English

检索标签内的所有信息:DOMNODE

[英]Retrieved all information inside tags : DOMNODE

I have a script in which I have no control over the HTML and can only modify the basic CSS.我有一个脚本,我无法控制 HTML,只能修改基本的 CSS。 I just wanted to know if it is possible to cut all the information from < script> until its closing </ script> without distinguishing between the elements inside?我只是想知道是否可以在不区分内部元素的情况下将所有信息从 <script> 剪切到其关闭 </script>?

Example:例子:

<script src='http://localhost/[...]/assets/js/frontend.min.js?ver=3.5.3'
    id='elementor-frontend-js'></script>

So for example here I would like to retrieve the whole line in string form to store it in an array.因此,例如在这里,我想以字符串形式检索整行以将其存储在数组中。

    $document = new DOMDocument();
    libxml_use_internal_errors(true);

    $document->loadHTML($content);
    libxml_use_internal_errors(false);

    // An empty array to store all the 'srcs'
    $scripts_array = [];

    // Store every script's source inside the array
    foreach ($document->getElementsByTagName('script') as $script) {
        if ($script->hasAttribute('src')) {
            $scripts_array[] = $script->getAttribute('src');
        }
    }

So here is a piece of my code, for now I manage to retrieve the loaded document, browse the scripts but I only pull the "src" attribute;所以这是我的一段代码,现在我设法检索加载的文档,浏览脚本,但我只提取“src”属性; but I want to retrieve all the content without distinction.但我想毫无区别地检索所有内容。

Thank you for your answers: :)谢谢您的回答: :)

You are getting only the src attribute value, because that's all you are asking for in $script->getAttribute('src') .您只获得src属性值,因为这就是您在$script->getAttribute('src')中要求的全部。 Replace your foreach with:将您的foreach替换为:

$xpath = new DomXPath($document);
$attribs = $xpath ->query("//script/@*");

foreach ($attribs as $attrib) {     
            $scripts_array[] = $attrib->nodeValue ;            
    }

and see if it works.看看它是否有效。

first of all thank you for your answer, I tried.首先谢谢你的回答,我试过了。 and I don't get what I want: When I do a query I get this:而且我没有得到我想要的:当我进行查询时,我得到了这个:

 "more_info": [
    "http://localhost:8888/Sadem/wordpress/wp-content/plugins/elementor/assets/lib/font-awesome/js/v4-shims.min.js?ver=3.5.5",
    "font-awesome-4-shim-js",
     ...and more
]

What I was looking for was having the complete line like this: "more_info": [ "http://localhost/.../assets/lib/font-awesome/js/v4-shims.min.js", "font-awesome-4-shim-js", ...and more ]我正在寻找的是这样的完整行:“more_info”:[“http://localhost/.../assets/lib/font-awesome/js/v4-shims.min.js”,“font -awesome-4-shim-js", ...等等]

What I was looking for was having the complete line like this:我一直在寻找的是这样的完整行:

"more_info": [
    "<link rel='stylesheet' href='http://localhost/.../assets/lib/font- 
     awesome/js/v4-shims.min.js' id='font-awesome-4-shim-js'/>",
     ...and more
] 

But I thank you again for your help, so few people want to help me....但是我再次感谢你的帮助,所以很少有人愿意帮助我......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM