Page 1 of 1
extracting url from messageboard into links
Posted: Wed Jul 24, 2002 11:18 am
by fred
Hi All,
I have a messageboard where users can enter comments.
I wish to change comments like:
Code: Select all
"Have a look at http://www.google.com its cool!"
into a linkable version:
Code: Select all
"Have a look at <a href="http://www.google.com">www.google.com</a> its cool!"
NOTE: the lack of
http://
----------------------------------------
I am aware from topic "1404" of:
Code: Select all
$text = preg_replace ("/(ftp|http|https):\/\/(їa-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\ї\]\|\*\$\^\{\}]+)/i", "<a href="\\1://\\2" target="_blank">\\1://\\2</a>", $text);
But this does not remove
"http://" from the url (it looks untidy) .
Code: Select all
"Have a look at <a href="http://www.google.com">http://www.google.com</a> its cool!"
It should be simple...

Posted: Wed Jul 24, 2002 11:21 am
by llimllib
If you don't know how to use regular expressions, use strpos to find the second occurence of http:// and substr to remove it. I ain't gonna write it for you, that's the fun of the game

Posted: Wed Jul 24, 2002 12:04 pm
by fred
llimllib wrote:If you don't know how to use regular expressions, use strpos to find the second occurence of http:// and substr to remove it. I ain't gonna write it for you, that's the fun of the game

Thanks for the point in the correct direction:
Code: Select all
$second=0;
while(1)
{
$first =strpos($text,'http://',$second);
$second =strpos($text,'http://',$first+1);
if ($first=== false)
break;
$textї$second]=' ';
$textї$second+1]=' ';
$textї$second+2]=' ';
$textї$second+3]=' ';
$textї$second+4]=' ';
$textї$second+5]=' ';
$textї$second+6]=' ';
}
I'm sure there is a nicer solution, but this is mine
it's a bit gruesome

Posted: Wed Jul 24, 2002 12:05 pm
by RandomEngy
Change the code to:
Code: Select all
$text = preg_replace ("/(ftp|http|https):\/\/(їa-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\ї\]\|\*\$\^\{\}]+)/i", "<a href="\\1://\\2" target="_blank">\\2</a>", $text);
Posted: Wed Jul 24, 2002 12:06 pm
by hex
no no no, just don't put it back into the code in the preg.
Change:
Code: Select all
$text = preg_replace ("/(ftp|http|https):\/\/(їa-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\ї\]\|\*\$\^\{\}]+)/i", "<a href="\\1://\\2" target="_blank">\\1://\\2</a>", $text);
To:
Code: Select all
$text = preg_replace ("/(ftp|http|https):\/\/(їa-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\ї\]\|\*\$\^\{\}]+)/i", "<a href="\\1://\\2" target="_blank">\\2</a>", $text);
Posted: Wed Jul 24, 2002 12:11 pm
by hex
RandomEngy wrote:Change the code to:
Code: Select all
$text = preg_replace ("/(ftp|http|https):\/\/(їa-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\ї\]\|\*\$\^\{\}]+)/i", "<a href="\\1://\\2" target="_blank">\\2</a>", $text);
beat me by a minute! i must've been writing slow!
Posted: Wed Jul 24, 2002 12:15 pm
by llimllib
Since you went out and wrote the script, here's a bit more elegant of a solution. Thanks for trying it yourself! you rock...
Code: Select all
$url = '<a href="http://www.google.com">http://www.google.com</a>';
//find the first http://
$pos = strpos($url, 'http://');
//then the second one
$pos = strpos($url, 'http://', $pos+1);
//now cut the string up to the start of the second http://
$link = substr($url, 0, $pos);
//now take everything after the second http://
$linkEnd = substr($url, $pos+7);
//and put them back together
$url = $link . $linkEnd;
echo htmlspecialchars($url);
The regular expression solutions are more elegant, but this one will show you how to use some string functions better.