Import div content from static HTML file using PHP or jQuery or Ajax?

Hi, a newbies familiar question ...

The problem is this:

I have a static HTML file and I want to import only part of this file to another page. How can i do this.

Code example:

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Some title here</title> <link rel="stylesheet" type="text/css" href="styles.css" /> </head> <body> <div id="box-1"> <div class="block"> <!-- Some code here --> </div> </div> <div id="box-2"> <div class="block"> <!-- Some code here --> </div> </div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script src="script.js"></script> </body> </html> 

Now I want to read this HTML file and import only this bit:

 <div id="box-1"> <div class="block"> <!-- Some code here --> </div> </div> 

Is there any way to do this?

Please help.

Even PHP, jQuery, Ajax or any other solution will also do. Please help me.

+4
source share
2 answers

You can use jQuery load () to specify only a specific container to load:

 $('#targetdiv').load('static.html #box-1'); 
+9
source

Here's a solution that is different from the others: it performs a one-time import of a content section from a large number of static html files / pages (in case that was what you wanted). I have successfully used it to import about 700 pages from direct html to the cms database.

 // get the pages you want to import $pages = array('page1.html', 'page2.html', 'page3.html' ); foreach($pages as $p) { $url = 'http://yourDomain.com/' . $p; // load the webpage $file = file_get_contents($url); if($file) { list($before,$content) = explode('<body>',$file); // chop off beginning unset($before); list($content) = explode('<div id="box-2">',$content); // chop off end; $resultArray[] = trim($content); // or do it this way to keep the filename associated with the content // $resultArray[$p] = $content; }//if file } //endforeach; // $resultArray holds your stripped content // do something with $resultArray; 
+4
source

Source: https://habr.com/ru/post/1333590/


All Articles