Results 1 to 1 of 1

Thread: Sort Script - Extract links, and sort (regex)

  1. #1
    Join Date
    Apr 2005
    Posts
    15

    Sort Script - Extract links, and sort (regex)

    I have a script that will read web file, extract the hyperlinks and sort them in alphabetical order.

    It works fine, but not the way I want.

    I want to change the script so that it will extract the text link as well.

    //sortlinks.php

    Code:
    <?php
        $matches= array();
        $page = file_get_contents("reviews.htm");
        $pattern = "/((?<=href=')).*?(?=')/i";
        preg_match_all($pattern,$page,$matches);
    
        $links[] = $matches[0];
    
        sort($links);
    
        $nlinks = $links[0];
        sort($nlinks);
    
        foreach ($nlinks as $l)
            echo '<a href="' . $l . '">' . $l . "</a><br />\n";
    ?>

    //reviews.htm the comments are to let you know the url of the links.
    - alink // directory/alink.htm
    - clink // director/clink.htm
    - blink // director/blink.htm

    Right now, the output I'm getting is

    directory/alink.htm
    directory/blink.htm
    directory/clink.htm

    This is what I want (as hyperlinks directing to their urls):
    alink
    blink
    clink

    I suspect we have to change something in $pattern.
    I've tried a lot of things, but I keep getting errors.

    I just can't come up with the logic to solve this.

    Any help would be appreciated.

    Thanks
    Last edited by matthewchan; 16 Feb 2006 at 04:49 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •