Home > OS >  Retrieved all information inside tags : DOMNODE
Retrieved all information inside tags : DOMNODE

Time:02-04

I have a script in which I have no control over the HTML and can only modify the basic 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?

First of all I use wp_remote_get (yes it is wordpress) :

$response = wp_remote_get("http:localhost/wp/?p={id}");
$content = response['body'];

$document = new DOMDocument();
$document->loadHTML($content);

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

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

Then for you to understand I return the array of all my scripts that I transmit. I get the array via an api.

Today with my script above I get this:

"more_info":{
     "http://localhost/wp/wp-content/plugins/elementor/assets/lib/font-awesome/js/v4-shims.min.js",
     "http://localhost/wp/wp-includes/js/comment-reply.min.js',
     [...] 
}

But I would like to be able to get this:

"more_info":{
     "<script src='http://localhost/wp/wp-content/plugins/elementor/assets/lib/font-awesome/js/v4-shims.min.js'id='font-awesome-4-shim-js'></script>",
     "<script src='http://localhost/wp/wordpress/wp-includes/js/comment-reply.min.js'id='comment-reply-js'></script>",
     [...] 
}

If you haven't understood yet, I'm browsing a page and I want to get all the lines and I want to put them in an array.

I hope it's clearer for you,

Thank you for your answers ! :)

CodePudding user response:

You are getting only the src attribute value, because that's all you are asking for in $script->getAttribute('src'). Replace your foreach with:

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

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

and see if it works.

CodePudding user response:

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 ]

What I was looking for was having the complete line like this:

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

But I thank you again for your help, so few people want to help me....

  •  Tags:  
  • Related