<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sportsfire</title>
    <description>The latest articles on DEV Community by sportsfire (@sportsfire).</description>
    <link>https://dev.to/sportsfire</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2215190%2F61ea10aa-bb35-4317-adb8-d00824e263c4.png</url>
      <title>DEV Community: sportsfire</title>
      <link>https://dev.to/sportsfire</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sportsfire"/>
    <language>en</language>
    <item>
      <title>php code for scrape links</title>
      <dc:creator>sportsfire</dc:creator>
      <pubDate>Wed, 16 Oct 2024 20:04:09 +0000</pubDate>
      <link>https://dev.to/sportsfire/php-code-for-scrape-links-2ej4</link>
      <guid>https://dev.to/sportsfire/php-code-for-scrape-links-2ej4</guid>
      <description>&lt;p&gt;To scrape links from a webpage using PHP, you can use the file_get_contents function to fetch the HTML content and then parse it using the DOMDocument class. Here's a simple example: Site : &lt;a href="https://sportsfire.app" rel="noopener noreferrer"&gt;SportsFire&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

// Function to scrape links from a given URL
function scrapeLinks($url) {
    // Get the HTML content of the webpage
    $html = file_get_contents($url);

    // Create a new DOMDocument instance
    $dom = new DOMDocument();

    // Suppress errors due to malformed HTML
    libxml_use_internal_errors(true);

    // Load the HTML content
    $dom-&amp;gt;loadHTML($html);

    // Clear the errors
    libxml_clear_errors();

    // Create an array to hold the links
    $links = [];

    // Get all &amp;lt;a&amp;gt; elements
    $anchors = $dom-&amp;gt;getElementsByTagName('a');

    // Loop through the anchors and collect the href attributes
    foreach ($anchors as $anchor) {
        $href = $anchor-&amp;gt;getAttribute('href');
        // Add the link to the array if it's not empty
        if (!empty($href)) {
            $links[] = $href;
        }
    }

    return $links;
}

// Example usage
$url = 'https://www.example.com'; // Change this to the URL you want to scrape
$links = scrapeLinks($url);

// Print the scraped links
foreach ($links as $link) {
    echo $link . PHP_EOL;
}
?&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>php</category>
    </item>
  </channel>
</rss>
