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
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
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:
Nope, already tried it. Maybe a clue: http://www.articledepot.co.uk/displayarticle.php?id=94 It links it as http://www.articledepot.co.uk/www.web-rover.com instead of www.web-rover.com
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.
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.
I refreshed and its close but not quite (take a look) http://www.articledepot.co.uk/displayarticle.php?id=94
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
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.
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:
Crap. Just remembered there's another instance of http: web rover which will break what I just wrote... Hold on.
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
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.
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.
Lol.. I realized my mistake and was fixing it while you were posting that code. Cheers T0PS - You're the man PM me where to send the $3 (want to give http://forums.digitalpoint.com/showthread.php?t=15817 a crack?)