Auto Parse URLs with PHP

Discussion in 'Web Design & Coding' started by thebear, Feb 6, 2003.

  1. thebear

    thebear d(-.-)b

    Messages:
    364
    Location:
    Toronto, Canadia
    I am opening a .txt file with the fopen function, and I want to know if there is a way of making all urls that are displayed links automatically
     
  2. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    Yeah it's possible but not trivial. This forums uses such a function. I really can't say how it's done off the top of my head, but if you seach google you might find some scripts that do this or at least have this function as a part of it.
     
  3. Smifffy

    Smifffy Guest

    try opening the file - making it a varible then just use echo

    <?php
    $page = "";
    $fp = fopen( "test.txt", "r" );
    while( !feof( $fp ) )
    $page .= fgets( $fp);
    fclose( $fp );

    echo "$page";
    ?>

    :p

    try that
     
  4. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    That will just display the contents of the file. There are functions that will do this for you. The problem was parsing any url:s into acctual links:

    Codewise:
    http://whatever.com =>
    <A HREF="http://whatever.com">http://whatever.com</A>

    Displaywise:
    http://whatever.com => http://whatever.com
     
  5. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    Just wrote this...
    PHP:
    <?php

    $p 
    fopen("test.txt""r");
    while(!
    feof($p))
    $output htmlspecialchars(fgets($p4096));
      
    $output preg_replace("/((http(s?):\/\/)|(www\.))([\w\.]+)/i""<a href=\"http$3://$4$5\" target=\"_blank\">$2$4$5</a>"$output);
      print 
    $output;
    }
    fclose($p);

    ?>
    It should work, Tried it locally on Windows, seems ok :)
     
  6. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    Oooh, I really need to learn about reg-expressions some time. They are very handy.
     
  7. Smifffy

    Smifffy Guest

    well when i ran it on my server it parsed them into links - strange

    o
    i see - silly me you want www.blabla.com into <a href="http://www.blabla.com">www.blabla.com</a>

    yea regex is the way to go then
     
  8. thebear

    thebear d(-.-)b

    Messages:
    364
    Location:
    Toronto, Canadia
    well, I'm probably doing some newbish thing to make this not work... when I run the script I get line after line of:
    Warning: Unknown modifier '/' in c:\apache\htdocs\nintel.php on line 54
     
  9. Zedric

    Zedric NTFS Guru Folding Team

    Messages:
    4,006
    Location:
    Sweden
    Yeah, I get that to. It's this stupid forum code parser again. Try this instead:
    PHP:
    <?php

    $p 
    fopen("test.txt""r");
    while(!
    feof($p))
    {
      
    $output htmlspecialchars(fgets($p4096));
      
    $output preg_replace("/((http(s?):\\\/\\\/)|(www\\\.))([\\\w\\\.]+)/i""<a href=\"http$3://$4$5\" target=\"_blank\">$2$4$5</a>"$output);
      print 
    $output;
    }
    fclose($p);

    ?>
     
  10. thebear

    thebear d(-.-)b

    Messages:
    364
    Location:
    Toronto, Canadia
    yeah, that works :cool:

    the only problem is on URLs that have a / after the .com, it doesnt add it...(ex. http://site.com/page.php)
     
  11. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    that has to do with the regexp. youd need to re-write it so that it will recognize everything beyon the .<tld>