Jump to content

[SOLVED] question about url detection


blufish

Recommended Posts

i wrote a similar script a while back here is a modified version

<?php
function link_url($url)
{
$url_href = substr($url, 0, 7) != "http://" ? "http://{$url}" : $url;
return "<a href=\"{$url_href}\" rel=\"nofollow\">{$url}</a>";
}
echo "<pre>";
$string = "hey guys check out this site at http://www.frozenoven.com http://www.test.com www.dsa.com www.asd.com";
//for http://
$count = substr_count($string, "http://");
$start = 0;
$len = 0;
for ($i = 1; $i <= $count; $i++) {
$start = strpos($string, "http://", $start + $len + 27);
$len = strpos($string . " ", " ", $start) - $start;
$url = substr($string, $start, $len);
$string = str_replace(" " . $url, " " . link_url($url), $string, $asd);
}
//for www
$count = substr_count($string, " www.");
$start = 0;
$len = 0;
for ($i = 1; $i <= $count; $i++) {
$start = strpos($string, " www.", $start + $len) + 1;
$len = strpos($string . " ", " ", $start) - $start;
$url = substr($string, $start, $len);
$string = str_replace(" " . $url, " " . link_url($url), $string, $asd);
}
var_dump($string);
?>

 

Scott.

what if the it's like

hey guys check out this site at http://www.frozenoven.com

i wrote a similar script a while back here is a modified version

<?php
function link_url($url)
{
$url_href = substr($url, 0, 7) != "http://" ? "http://{$url}" : $url;
return "<a href=\"{$url_href}\" rel=\"nofollow\">{$url}</a>";
}
echo "<pre>";
$string = "hey guys check out this site at http://www.frozenoven.com http://www.test.com www.dsa.com www.asd.com";
//for http://
$count = substr_count($string, "http://");
$start = 0;
$len = 0;
for ($i = 1; $i <= $count; $i++) {
$start = strpos($string, "http://", $start + $len + 27);
$len = strpos($string . " ", " ", $start) - $start;
$url = substr($string, $start, $len);
$string = str_replace(" " . $url, " " . link_url($url), $string, $asd);
}
//for www
$count = substr_count($string, " www.");
$start = 0;
$len = 0;
for ($i = 1; $i <= $count; $i++) {
$start = strpos($string, " www.", $start + $len) + 1;
$len = strpos($string . " ", " ", $start) - $start;
$url = substr($string, $start, $len);
$string = str_replace(" " . $url, " " . link_url($url), $string, $asd);
}
var_dump($string);
?>

 

Scott.

Or you could just do:

<?php
$subject = "hey guys check out this site at http://www.frozenoven.com";
preg_match('/\\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/i', $subject,$matches);
echo $matches[0];
?>

Will echo http://www.frozenoven.co ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.