Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6069029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:46:42+00:00 2026-05-23T09:46:42+00:00

I am writing a code is a crawler but I want it to crawl

  • 0

I am writing a code is a crawler but I want it to crawl all the links that have the same base. For example if you set a big depth and you have a link in your page that links to your twitter, it will scan twitter and give you results like twitter.com/xxxyyyzzz.

What I want is to restrict the code to crawl only the urls that have the same base. I don’t mind if I set the domain again in a new variable.

Filtering the results and showing only the correct links at the end is not the appropriate way. Imagine if you find 1000 links and you just want the 10.

Thank you for the ideas.
(the correct code is in the answer)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:46:42+00:00Added an answer on May 23, 2026 at 9:46 am

    MODIFIED

    Try this on for size

    function crawl_page($url, $depth = 2) {
        static $seen = array();
        if (isset($seen[$url]) || $depth == 0) {
            return;
        }
        
        $seen[$url] = true;
        $parts = parse_url($url);
        $dom = new DOMDocument('1.0');
        if (!$parts || !@$dom->loadHTMLFile($url)) {
            return;
        }
        
        $anchors = $dom->getElementsByTagName('a');
        foreach ($anchors as $anchor) {
            $href = $anchor->getAttribute('href');
            $path = false;
            if (0 !== strpos($href, 'http') && 0 !== strpos($href, 'javascript:')) {
                $path = '/' . ltrim($href, '/');
                if (extension_loaded('http')) {
                    $path = http_build_url($url, array('path' => $path));
                }
                else {
                    $href = "{$parts['scheme']}://";
                    if (isset($parts['user'])) {
                        $href .= $parts['user'];
                        if (isset($parts['pass'])) {
                            $href .= ":{$parts['pass']}";
                        }
                        $href .= '@';
                    }
                    $href .= $parts['host'];
                    if (isset($parts['port'])) {
                        $href .= ':' . $parts['port'];
                    }
                    $path = $href . $path;
                }
            }
            else {
                $href_parts = parse_url($href);
                if($href_parts['host'] == $parts['host'] && $href_parts['scheme'] == $parts['scheme']) {
                    $path = $href;
                }
            }
            if (!empty($path) && $depth - 1 != 0) {
                crawl_page($path, $depth - 1);
            }
        }
        echo "Crawled: {$url}\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing code that automatically generates HTML, and I want it to encode things
If you are writing code in a class that inherits from a base class
I'm writing a little crawler that should fetch a URL multiple times, I want
While writing code in a file that would comprise of PHP, HTML, CSS &
I'm writing code that looks similar to this: public IEnumerable<T> Unfold<T>(this T seed) {
I'm just getting started writing a simple web crawler to get info on links
I am writing code that will populate the Margin , Padding and BorderThickness properties
When writing code in JScript, as I am wont to do as I have
I am writing code that involved a for loop that makes calculations at each
When writing code, especially when dealing with dates and times, you have to work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.