Get all links from a page

PythonPython Forum LeaderThe Royal RAM
I want to have a text box where a user would type in the URL to a website or specific page... Then upon pressing submit the script would find all links on that page, including image links and echo them all in a list...

I think this has got something to do with fopen() to open up the webpage as a file and then fclose() to close it again... Not sure though ;)

Any ideas?

Thanks

The Royal Ram

Comments

  • FelixFelix Junior Member Shared Hoster
    Edit: Damn there appears to be some problems with the code I never realised before. I'll fix those up ASAP for you.

    Let's break down what we need to do first of all:

    1. Read all data from the file - Simply read the data from the file and place it into a variable in our PHP script.

    2. Search for all possible link tags - We will need to use preg in order to perform a process of identifying all the possible link tags in a page. Link tags can be broken down and identified by A and IMG.

    3. Store link tags into an array.

    4. Loop through the array and output the links.

    Okay so now what we can do is create our own class that can be used to manipulate data on a page. I have decided to show a class here for you as you can enhance it with many more file-handling functions yourself. :D

    Here is the class with an explanation of each part.

    Edit: Removed. I need to fix a few things for you. Sorry. Can't do them right now though.
  • ChroderChroder Senior Member The Royal RAM
    You can still open a file for reading using fopen if it is on a remote server if allow_url_fopen is enabled in php.ini.

    Take a look at the section on using remote files.
  • FelixFelix Junior Member Shared Hoster
    Chroder wrote:
    You can still open a file for reading using fopen if it is on a remote server if allow_url_fopen is enabled in php.ini.

    Take a look at the section on using remote files.

    Yep I forgot about that. I was getting confused with something else. :P
  • FelixFelix Junior Member Shared Hoster
    *BUMP*

    Sorry man I keep forgetting to do this. Perhaps you would be quicker to research yourself instead of waiting for me. What you need to do is learn about regular expressions. Simply do a search on google for "regular expressions PHP" and you should be able to find what you need.
Sign In or Register to comment.