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}");
This is what response looks like:
array(6) { ["headers"]=> object(Requests_Utility_CaseInsensitiveDictionary)#6325 (1) { ["data":protected]=> array(6) { ["date"]=> string(29) "Fri, 04 Feb 2022 08:27:59 GMT" ["server"]=> string(141) "Apache/2.4.48 (Unix) OpenSSL/1.0.2u PHP/8.0.8 mod_wsgi/3.5 Python/2.7.13 mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_perl/2.0.11 Perl/v5.30.1" ["x-powered-by"]=> string(9) "PHP/8.0.8" ["x-pingback"]=> string(48) "http://localhost:8888/Sadem/wordpress/xmlrpc.php" ["link"]=> array(3) { [0]=> string(74) "; rel="https://api.w.org/"" [1]=> string(104) "; rel="alternate"; type="application/json"" [2]=> string(60) "; rel=shortlink" } ["content-type"]=> string(24) "text/html; charset=UTF-8" } } ["body"]=> string(49331) "
And the body ($response["body"]) contains a very simple html page:
- Links with stylesheets
- Tags html
- Links with scripts
And the script :
$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 ! 🙂
2
Answers
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:
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:
But I thank you again for your help, so few people want to help me....
You are getting only the
src
attribute value, because that’s all you are asking for in$script->getAttribute('src')
. Replace yourforeach
with:and see if it works.