extracting url from messageboard into links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fred
Forum Newbie
Posts: 7
Joined: Wed Jul 24, 2002 11:18 am
Location: England / Deutschland

extracting url from messageboard into links

Post 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):\/\/(&#1111;a-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\&#1111;\]\|\*\$\^\&#123;\&#125;]+)/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... 8O
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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 :twisted:
User avatar
fred
Forum Newbie
Posts: 7
Joined: Wed Jul 24, 2002 11:18 am
Location: England / Deutschland

Post 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 :twisted:
Thanks for the point in the correct direction:

Code: Select all

$second=0;
   while(1)
   &#123;
      $first  =strpos($text,'http://',$second);
      $second =strpos($text,'http://',$first+1);

      if ($first=== false)
           break;
      $text&#1111;$second]=' ';
      $text&#1111;$second+1]=' ';
      $text&#1111;$second+2]=' ';
      $text&#1111;$second+3]=' ';
      $text&#1111;$second+4]=' ';
      $text&#1111;$second+5]=' ';
      $text&#1111;$second+6]=' ';
   &#125;
I'm sure there is a nicer solution, but this is mine :)

Code: Select all

$text&#1111;$second+ ... ]=' ';
it's a bit gruesome :!:
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

Change the code to:

Code: Select all

$text = preg_replace ("/(ftp|http|https):\/\/(&#1111;a-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\&#1111;\]\|\*\$\^\&#123;\&#125;]+)/i", "<a href="\\1://\\2" target="_blank">\\2</a>", $text);
User avatar
hex
Forum Commoner
Posts: 92
Joined: Sat Apr 20, 2002 3:20 am
Location: UK

Post 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):\/\/(&#1111;a-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\&#1111;\]\|\*\$\^\&#123;\&#125;]+)/i", "<a href="\\1://\\2" target="_blank">\\1://\\2</a>", $text);
To:

Code: Select all

$text = preg_replace ("/(ftp|http|https):\/\/(&#1111;a-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\&#1111;\]\|\*\$\^\&#123;\&#125;]+)/i", "<a href="\\1://\\2" target="_blank">\\2</a>", $text);
User avatar
hex
Forum Commoner
Posts: 92
Joined: Sat Apr 20, 2002 3:20 am
Location: UK

Post by hex »

RandomEngy wrote:Change the code to:

Code: Select all

$text = preg_replace ("/(ftp|http|https):\/\/(&#1111;a-z0-9~#%@&:;=!',_crl\(\)\?\/\.\-\+\&#1111;\]\|\*\$\^\&#123;\&#125;]+)/i", "<a href="\\1://\\2" target="_blank">\\2</a>", $text);
beat me by a minute! i must've been writing slow!
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Post Reply