Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

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 230 views
2 answers 253 views
1 answer 255 views
1 answer 194 views
1 answer 214 views
2 answers 168 views
...