3v4l.org

run code in 300+ PHP versions simultaneously
<?php $hrefPattern = '/<a\\s+[^>]*href=(["\']??)([^"\'>]*?)\\1([^>]*)>(.*)<\/a>/siU'; $html = <<<HTML <p>If you find any cases where this code falls down, let us know using the Feedback link below.</p> <p>Before using this or similar scripts to fetch pages from other websites, we suggest you read through the related article on <a href="/php/parse-robots/">setting a user agent and parsing robots.txt</a>.</p> <h2>First checking robots.txt</h2> <p>As mentioned above, before using a script to download files you should always <a href="/php/parse-robots/">check the robots.txt file</a>. Here we're making use of the <tt>robots_allowed</tt> function from the article linked above to determine whether we're allowed to access files:</p> <code class="final">&lt;?PHP <i>// Original PHP code by Chirp Internet: www.chirp.com.au // Please acknowledge use of this code by including this header.</i> <span> ini_set('user_agent', '<i>NameOfAgent (http://www.example.net)</i>');</span> $url = &quot;http://www.example.net/somepage.html&quot;; <span> if(robots_allowed($url, &quot;<i>NameOfAgent</i>&quot;)) {</span> $input = @file_get_contents($url) or die(&quot;Could not access file: $url&quot;); $regexp = &quot;<tt>&lt;a\s[^&gt;]*href=(\&quot;??)([^\&quot; &gt;]*?)\\1[^&gt;]*&gt;(.*)&lt;\/a&gt;</tt>&quot;; if(preg_match_all(&quot;/$regexp/siU&quot;, $input, $matches, PREG_SET_ORDER)) { foreach($matches as $match) { <i>// $match[2] = link address // $match[3] = link text</i> } } <span> } else { die('Access denied by robots.txt'); }</span> ?&gt;</code> HTML; preg_match_all($hrefPattern, $html, $matches); var_dump($matches);
Output for 8.3.0 - 8.3.6
Warning: Undefined variable $url in /in/c3qaN on line 27 Warning: Undefined variable $url in /in/c3qaN on line 28 Warning: Undefined variable $input in /in/c3qaN on line 29 Warning: Undefined variable $url in /in/c3qaN on line 29 Warning: Undefined variable $url in /in/c3qaN on line 29 Warning: Undefined variable $regexp in /in/c3qaN on line 30 Warning: Undefined variable $regexp in /in/c3qaN on line 31 Warning: Undefined variable $input in /in/c3qaN on line 31 Warning: Undefined variable $matches in /in/c3qaN on line 31 Warning: Undefined variable $matches in /in/c3qaN on line 32 Warning: Undefined variable $match in /in/c3qaN on line 32 Warning: Undefined variable $match in /in/c3qaN on line 33 Warning: Trying to access array offset on null in /in/c3qaN on line 33 Warning: Undefined variable $match in /in/c3qaN on line 34 Warning: Trying to access array offset on null in /in/c3qaN on line 34 array(5) { [0]=> array(2) { [0]=> string(76) "<a href="/php/parse-robots/">setting a user agent and parsing robots.txt</a>" [1]=> string(59) "<a href="/php/parse-robots/">check the robots.txt file</a>" } [1]=> array(2) { [0]=> string(1) """ [1]=> string(1) """ } [2]=> array(2) { [0]=> string(18) "/php/parse-robots/" [1]=> string(18) "/php/parse-robots/" } [3]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [4]=> array(2) { [0]=> string(43) "setting a user agent and parsing robots.txt" [1]=> string(26) "check the robots.txt file" } }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
Warning: Undefined variable $url in /in/c3qaN on line 27 Warning: Undefined variable $url in /in/c3qaN on line 28 Warning: Undefined variable $input in /in/c3qaN on line 29 Warning: Undefined variable $url in /in/c3qaN on line 29 Warning: Undefined variable $url in /in/c3qaN on line 29 Warning: Undefined variable $regexp in /in/c3qaN on line 30 Warning: Undefined variable $regexp in /in/c3qaN on line 31 Warning: Undefined variable $input in /in/c3qaN on line 31 Warning: Undefined variable $matches in /in/c3qaN on line 31 Warning: Undefined variable $matches in /in/c3qaN on line 32 Warning: Undefined variable $match in /in/c3qaN on line 32 Warning: Undefined variable $match in /in/c3qaN on line 33 Warning: Trying to access array offset on value of type null in /in/c3qaN on line 33 Warning: Undefined variable $match in /in/c3qaN on line 34 Warning: Trying to access array offset on value of type null in /in/c3qaN on line 34 array(5) { [0]=> array(2) { [0]=> string(76) "<a href="/php/parse-robots/">setting a user agent and parsing robots.txt</a>" [1]=> string(59) "<a href="/php/parse-robots/">check the robots.txt file</a>" } [1]=> array(2) { [0]=> string(1) """ [1]=> string(1) """ } [2]=> array(2) { [0]=> string(18) "/php/parse-robots/" [1]=> string(18) "/php/parse-robots/" } [3]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [4]=> array(2) { [0]=> string(43) "setting a user agent and parsing robots.txt" [1]=> string(26) "check the robots.txt file" } }
Output for 7.4.0 - 7.4.33
Notice: Undefined variable: url in /in/c3qaN on line 27 Notice: Undefined variable: url in /in/c3qaN on line 28 Notice: Undefined variable: input in /in/c3qaN on line 29 Notice: Undefined variable: url in /in/c3qaN on line 29 Notice: Undefined variable: url in /in/c3qaN on line 29 Notice: Undefined variable: regexp in /in/c3qaN on line 30 Notice: Undefined variable: regexp in /in/c3qaN on line 31 Notice: Undefined variable: input in /in/c3qaN on line 31 Notice: Undefined variable: matches in /in/c3qaN on line 31 Notice: Undefined variable: matches in /in/c3qaN on line 32 Notice: Undefined variable: match in /in/c3qaN on line 32 Notice: Undefined variable: match in /in/c3qaN on line 33 Notice: Trying to access array offset on value of type null in /in/c3qaN on line 33 Notice: Undefined variable: match in /in/c3qaN on line 34 Notice: Trying to access array offset on value of type null in /in/c3qaN on line 34 array(5) { [0]=> array(2) { [0]=> string(76) "<a href="/php/parse-robots/">setting a user agent and parsing robots.txt</a>" [1]=> string(59) "<a href="/php/parse-robots/">check the robots.txt file</a>" } [1]=> array(2) { [0]=> string(1) """ [1]=> string(1) """ } [2]=> array(2) { [0]=> string(18) "/php/parse-robots/" [1]=> string(18) "/php/parse-robots/" } [3]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [4]=> array(2) { [0]=> string(43) "setting a user agent and parsing robots.txt" [1]=> string(26) "check the robots.txt file" } }
Output for 7.3.32 - 7.3.33
array(5) { [0]=> array(2) { [0]=> string(76) "<a href="/php/parse-robots/">setting a user agent and parsing robots.txt</a>" [1]=> string(59) "<a href="/php/parse-robots/">check the robots.txt file</a>" } [1]=> array(2) { [0]=> string(1) """ [1]=> string(1) """ } [2]=> array(2) { [0]=> string(18) "/php/parse-robots/" [1]=> string(18) "/php/parse-robots/" } [3]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [4]=> array(2) { [0]=> string(43) "setting a user agent and parsing robots.txt" [1]=> string(26) "check the robots.txt file" } }
Output for 5.0.4 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.2, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31
Notice: Undefined variable: url in /in/c3qaN on line 27 Notice: Undefined variable: url in /in/c3qaN on line 28 Notice: Undefined variable: input in /in/c3qaN on line 29 Notice: Undefined variable: url in /in/c3qaN on line 29 Notice: Undefined variable: url in /in/c3qaN on line 29 Notice: Undefined variable: regexp in /in/c3qaN on line 30 Notice: Undefined variable: regexp in /in/c3qaN on line 31 Notice: Undefined variable: input in /in/c3qaN on line 31 Notice: Undefined variable: matches in /in/c3qaN on line 31 Notice: Undefined variable: matches in /in/c3qaN on line 32 Notice: Undefined variable: match in /in/c3qaN on line 32 Notice: Undefined variable: match in /in/c3qaN on line 33 Notice: Undefined variable: match in /in/c3qaN on line 34 array(5) { [0]=> array(2) { [0]=> string(76) "<a href="/php/parse-robots/">setting a user agent and parsing robots.txt</a>" [1]=> string(59) "<a href="/php/parse-robots/">check the robots.txt file</a>" } [1]=> array(2) { [0]=> string(1) """ [1]=> string(1) """ } [2]=> array(2) { [0]=> string(18) "/php/parse-robots/" [1]=> string(18) "/php/parse-robots/" } [3]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [4]=> array(2) { [0]=> string(43) "setting a user agent and parsing robots.txt" [1]=> string(26) "check the robots.txt file" } }
Output for 5.2.3 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Notice: Undefined variable: url in /in/c3qaN on line 28 Notice: Undefined variable: url in /in/c3qaN on line 29 Notice: Undefined variable: input in /in/c3qaN on line 29 Notice: Undefined variable: url in /in/c3qaN on line 29 Notice: Undefined variable: url in /in/c3qaN on line 30 Notice: Undefined variable: regexp in /in/c3qaN on line 31 Notice: Undefined variable: regexp in /in/c3qaN on line 31 Notice: Undefined variable: input in /in/c3qaN on line 31 Notice: Undefined variable: matches in /in/c3qaN on line 32 Notice: Undefined variable: matches in /in/c3qaN on line 32 Notice: Undefined variable: match in /in/c3qaN on line 33 Notice: Undefined variable: match in /in/c3qaN on line 33 Notice: Undefined variable: match in /in/c3qaN on line 34 array(5) { [0]=> array(2) { [0]=> string(76) "<a href="/php/parse-robots/">setting a user agent and parsing robots.txt</a>" [1]=> string(59) "<a href="/php/parse-robots/">check the robots.txt file</a>" } [1]=> array(2) { [0]=> string(1) """ [1]=> string(1) """ } [2]=> array(2) { [0]=> string(18) "/php/parse-robots/" [1]=> string(18) "/php/parse-robots/" } [3]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [4]=> array(2) { [0]=> string(43) "setting a user agent and parsing robots.txt" [1]=> string(26) "check the robots.txt file" } }
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.3
Notice: Undefined variable: url in /in/c3qaN on line 27 Notice: Undefined variable: url in /in/c3qaN on line 28 Notice: Undefined variable: input in /in/c3qaN on line 29 Notice: Undefined variable: url in /in/c3qaN on line 29 Notice: Undefined variable: url in /in/c3qaN on line 29 Notice: Undefined variable: regexp in /in/c3qaN on line 30 Notice: Undefined variable: regexp in /in/c3qaN on line 31 Notice: Undefined variable: input in /in/c3qaN on line 31 Notice: Undefined variable: matches in /in/c3qaN on line 31 Notice: Undefined variable: matches in /in/c3qaN on line 32 Notice: Undefined variable: match in /in/c3qaN on line 32 Notice: Undefined variable: match in /in/c3qaN on line 33 Notice: Undefined variable: match in /in/c3qaN on line 34 array(5) { [0]=> array(2) { [0]=> string(76) "<a href="/php/parse-robots/">setting a user agent and parsing robots.txt</a>" [1]=> string(59) "<a href="/php/parse-robots/">check the robots.txt file</a>" } [1]=> array(2) { [0]=> string(1) """ [1]=> string(1) """ } [2]=> array(2) { [0]=> string(18) "/php/parse-robots/" [1]=> string(18) "/php/parse-robots/" } [3]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [4]=> array(2) { [0]=> string(43) "setting a user agent and parsing robots.txt" [1]=> string(26) "check the robots.txt file" } }

preferences:
309.05 ms | 405 KiB | 460 Q