1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Converting www. to a live link

Discussion in 'PHP' started by yfs1, May 10, 2005.

  1. #1
    This preg_match_all command is converting http:// to a live link but not www.

    preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $article, $match);
    PHP:
    Can anyone tell me where the error is?

    Cheers
     
    yfs1, May 10, 2005 IP
  2. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #2
    No response so I will give $3 to the first person who can provide a cut and paste fix for this issue.

    Combine that with this thread and you can make some money for a few minutes work.

    Payment will be via Paypal
     
    yfs1, May 11, 2005 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can you post the typical contents of $article and $match so we have something to play with?
     
    T0PS3O, May 11, 2005 IP
  4. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here is the code in its entirety for reference

    <?php
    
    
    
    include "conn.php";
    
    $sq ="SELECT * FROM articles WHERE id = '$id'";
    $rst = mysql_query($sq, $db) or die (mysql_error());
    
    $datas = mysql_fetch_array($rst);
    $pagetitle = "$datas[title]";
    
    $article = strip_tags($datas[article]);
    //preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)*w{3}\.[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $article, $match); 
    
    
    preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $article, $match); 
    
    foreach( $match[0] as $url ) 
    { 
    $article = str_replace( $url, "<a href=\"$url\">$url</a>", $article); 
    } 
    
    
    
    
    
    
    $author = strip_tags($datas[author]);
    
    preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)*w{3}\.[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $author, $matches);
    
    
    
    foreach( $matches[0] as $urls ) 
    
    { 
    $author = str_replace( $urls, "<a href=\"$urls\">$urls</a>", $author); 
    } 
    
    
    $contents ="<center> <table width = '90%'><tr><td width ='100%'> <h1 align='center'> <font size = 4> <b> $datas[title] </b></h1></td></tr>
    <tr><td> " .nl2br($article). "</td></tr><tr><td> <p> <b>Author Info: </b> <p> $author </td></tr></table>";
    
    include_once "template.php";
    
    ?>
    PHP:
     
    yfs1, May 11, 2005 IP
  5. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Beats me (at a glance). Try a dot after the www .
     
    T0PS3O, May 11, 2005 IP
  6. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yfs1, May 11, 2005 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OK if that's it I know why. It makes a link to www without the http so it will make it relative to the current dorectory. Yuo will have to make it that if it's a www match then append http:// in front of it. Will make a quick attempt to fix that code for you now.
     
    T0PS3O, May 11, 2005 IP
  8. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You have now:

    
    foreach( $match[0] as $url )
    {
    $article = str_replace( $url, "<a href=\"$url\">$url</a>", $article);
    } 
    
    PHP:
    And should be something like:

    
    foreach( $match[0] as $url )
    {
    if(substr($url,0,3) == "www")
    {
    $article = str_replace( $url, "<a href=\"http://" . $url . "\">$url</a>", $article);
    }
    else
    {
    $article = str_replace( $url, "<a href=\"$url\">$url</a>", $article);
    }
    } 
    
    PHP:
    Try that.

    PS Make sure yuo refresh this, yuo were already back here but I changed a bit.
     
    T0PS3O, May 11, 2005 IP
  9. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yfs1, May 11, 2005 IP
  10. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It seems to loop through it twice... Will check.
     
    T0PS3O, May 11, 2005 IP
  11. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #11
    If it offers another clue, the second link on the page works but if you scroll down, the http:// no longer works

    Thanks for the help so far T0PS
     
    yfs1, May 11, 2005 IP
  12. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The preg match finds multiple instance of that web-rover link. So the foreach will loop through each of them breaking the previously generated correct one.

    We'll need to make a check that this one is deleted from the match array. I'll find the function to delete duplicates.
     
    T0PS3O, May 11, 2005 IP
  13. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Try this. Same code but deletes duplicates first and re-indexes the array.

    
    
    $temp=array_unique($match);
    $match=array_values($temp);
    
    foreach( $match[0] as $url )
    {
    if(substr($url,0,3) == "www")
    {
    $article = str_replace( $url, "<a href=\"http://" . $url . "\">$url</a>", $article);
    array_splice($match, array_search("http://" . $url . "", $match), 1);
    }
    else
    {
    $article = str_replace( $url, "<a href=\"$url\">$url</a>", $article);
    }
    } 
    
    PHP:
     
    T0PS3O, May 11, 2005 IP
  14. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #14
    No, it gets the same result
     
    yfs1, May 11, 2005 IP
  15. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Crap. Just remembered there's another instance of http: web rover which will break what I just wrote... Hold on.
     
    T0PS3O, May 11, 2005 IP
  16. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Try new code in previous post. Isn't case insensitive yet but will try and do that.
     
    T0PS3O, May 11, 2005 IP
  17. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #17
    It works for the first link but not the one in the Author: box

    Just to make sure I am pasting it correctly

    <?php
    
    
    
    include "conn.php";
    
    $sq ="SELECT * FROM articles WHERE id = '$id'";
    $rst = mysql_query($sq, $db) or die (mysql_error());
    
    $datas = mysql_fetch_array($rst);
    $pagetitle = "$datas[title]";
    
    $article = strip_tags($datas[article]);
    //preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)*w{3}\.[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $article, $match); 
    
    
    preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $article, $match); 
    
    $temp=array_unique($match); 
    $match=array_values($temp); 
    
    foreach( $match[0] as $url ) 
    { 
    if(substr($url,0,3) == "www") 
    { 
    $article = str_replace( $url, "<a href=\"http://" . $url . "\">$url</a>", $article); 
    array_splice($match, array_search("http://" . $url . "", $match), 1); 
    } 
    else 
    { 
    $article = str_replace( $url, "<a href=\"$url\">$url</a>", $article); 
    } 
    }  
    
    
    
    
    
    
    $author = strip_tags($datas[author]);
    
    preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)*w{3}\.[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $author, $matches);
    
    
    
    $temp=array_unique($match); 
    $match=array_values($temp); 
    
    foreach( $match[0] as $url ) 
    { 
    if(substr($url,0,3) == "www") 
    { 
    $article = str_replace( $url, "<a href=\"http://" . $url . "\">$url</a>", $article); 
    array_splice($match, array_search("http://" . $url . "", $match), 1); 
    } 
    else 
    { 
    $article = str_replace( $url, "<a href=\"$url\">$url</a>", $article); 
    } 
    }  
    
    
    
    $contents ="<center> <table width = '90%'><tr><td width ='100%'> <h1 align='center'> <font size = 4> <b> $datas[title] </b></h1></td></tr>
    <tr><td> " .nl2br($article). "</td></tr><tr><td> <p> <b>Author Info: </b> <p> $author </td></tr></table>";
    
    
    include_once "template.php";
    
    ?>
    PHP:
    It is producing an error though
     
    yfs1, May 11, 2005 IP
  18. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #18
    You've got the old pregmatch in author with the *w{3} . Change that to the one of the article. Also note how the $match changes to $matches for authors so you'll have to change my code for authors to reflect that.
     
    T0PS3O, May 11, 2005 IP
  19. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #19
    This should do the trick:

    
    <?php
    
    
    
    include "conn.php";
    
    $sq ="SELECT * FROM articles WHERE id = '$id'";
    $rst = mysql_query($sq, $db) or die (mysql_error());
    
    $datas = mysql_fetch_array($rst);
    $pagetitle = "$datas[title]";
    
    $article = strip_tags($datas[article]);
    //preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)*w{3}\.[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $article, $match);
    
    
    preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $article, $match);
    
    $temp=array_unique($match);
    $match=array_values($temp);
    
    foreach( $match[0] as $url )
    {
    if(substr($url,0,3) == "www")
    {
    $article = str_replace( $url, "<a href=\"http://" . $url . "\">$url</a>", $article);
    array_splice($match, array_search("http://" . $url . "", $match), 1);
    }
    else
    {
    $article = str_replace( $url, "<a href=\"$url\">$url</a>", $article);
    }
    }  
    
    
    
    
    
    
    $author = strip_tags($datas[author]);
    
    preg_match_all("/(http:\/\/|ftp:\/\/|https:\/\/|www)[^\s]+\.(com|co|org|net|info|gov|biz|[a-z]{2}|[a-z]{4}|[a-z]{3})(\.[a-z]{2})*(\/[a-zA-Z0-9]*)*/", $author, $matches);
    
    
    
    $temp=array_unique($matches);
    $matches=array_values($temp);
    
    foreach( $matches[0] as $url )
    {
    if(substr($url,0,3) == "www")
    {
    $article = str_replace( $url, "<a href=\"http://" . $url . "\">$url</a>", $article);
    array_splice($matches, array_search("http://" . $url . "", $matches), 1);
    }
    else
    {
    $article = str_replace( $url, "<a href=\"$url\">$url</a>", $article);
    }
    }  
    
    
    
    $contents ="<center> <table width = '90%'><tr><td width ='100%'> <h1 align='center'> <font size = 4> <b> $datas[title] </b></h1></td></tr>
    <tr><td> " .nl2br($article). "</td></tr><tr><td> <p> <b>Author Info: </b> <p> $author </td></tr></table>";
    
    
    include_once "template.php";
    
    ?> 
    
    PHP:
    I see it works charming... EDIT: No we're back to the relative path.
     
    T0PS3O, May 11, 2005 IP
  20. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #20
    yfs1, May 11, 2005 IP