How to read HTML source of a URL into an array in PHP

2 Answers

0 votes
$arr = file('http://www.example.com/');

foreach ($arr as $line_numer => $line) 
    echo "Line {$line_numer}: " . htmlspecialchars($line) . "<br />";



/*
run: 

Line 0: <!doctype html> 
Line 1: <html> 
Line 2: <head> 
Line 3: <title>Example Domain</title> 
Line 4: 
Line 5: <meta charset="utf-8" /> 
Line 6: <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
Line 7: <meta name="viewport" content="width=device-width, initial-scale=1" /> 
Line 8: <style type="text/css"> 
Line 9: body { 
Line 10: background-color: #f0f0f2; 
...

*/

 



answered Jun 16, 2016 by avibootz
edited Jun 16, 2016 by avibootz
0 votes
$arr = file('http://www.example.com/');
 
print_r($arr);

/*
run: 

Array
(
    [0] => <!doctype html>

    [1] => <html>

    [2] => <head>

    [3] =>     <title>Example Domain</title>

    [4] => 

    [5] =>     <meta charset="utf-8" />

    [6] =>     <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

    [7] =>     <meta name="viewport" content="width=device-width, initial-scale=1" />

    [8] =>     <style type="text/css">

    [9] =>     body {

    [10] =>         background-color: #f0f0f2;

    [11] =>         margin: 0;

    [12] =>         padding: 0;

    [13] =>         font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    [14] =>         

    [15] =>     }

    [16] =>     div {

    [17] =>         width: 600px;
    
    ...

*/

 



answered Jun 16, 2016 by avibootz

Related questions

1 answer 263 views
2 answers 272 views
1 answer 283 views
1 answer 223 views
1 answer 238 views
2 answers 194 views
...