SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    convert links to <a> tags code

    hey guys, i got a code that convert links entered through a textarea to a clickable link with page title as shown string
    like this:
    <a href="enteredURL" rel="nofollow">pageTitle</a>

    I wanna know if the code below looks all right to you? it works only for some links not all, and i dont know the reason.

    Code PHP:
    function get_page_title($url)
    {
    	ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11');
    	if(!($data = @file_get_contents($url)))
    		return false;
    	if(preg_match("#<title>(.+)<\/title>#iU", $data, $t))
    		return trim($t[1]);
    	return false;
    }
    function _make_url_clickable_cb($matches) 
    {
    	$ret = '';
    	$url = $matches[2];
    	if (empty($url))
    		return $matches[0];
    	if (in_array(substr($url, -1), array('.', ',', ';', ':')) === true) 
    	{
    		$ret = substr($url, -1);
    		$url = substr($url, 0, strlen($url)-1);
    	}
    	$x = get_page_title($url);
    	if (!$x)
    		$x = 'link';
    	return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$x</a>" . $ret;
    }
    function _make_web_ftp_clickable_cb($matches) 
    {
    	$ret = '';
    	$dest = $matches[2];
    	$dest = 'http://' . $dest;
    	if (empty($dest))
    		return $matches[0];
    	if (in_array(substr($dest, -1), array('.', ',', ';', ':')) === true) 
    	{
    		$ret = substr($dest, -1);
    		$dest = substr($dest, 0, strlen($dest)-1);
    	}
    	$x = get_page_title($dest);
    	if (!$x)
    		$x = 'link';
    	return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$x</a>" . $ret;
    }
    function make_clickable($ret)
    {
    	$ret = ' ' . $ret;
    	$ret = preg_replace_callback('#([\s>])([\w]+?://[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
    	$ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
    	$ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
    	$ret = trim($ret);
    	return $ret;
    }

    An example of link which it fails to convert is this:
    http://www.yad2.co.il/Order/Order2_Pet.php

    The result given for the link above is:
    <a href="http://www.yad2.co.il/Order/Order2_Pet.php" rel="nofollow">

    so it gets stucked somewhere at getting the title .... any help?

    Thanks,
    ulthane

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    A quick guess is because the page title of your given link is in hebrew !!!
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Nope thats not that my whole site is in hebrew, and there are links with hebrew titles that do work aswell...
    I got no clue what the problem can be....

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    have you tried making a page with the same title, then removing characters one by one to see which is causing the problem ?
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    can it be related somehow to timeout or so?
    i mean, for some links like the one i gave above i noticed it takes a while for the proccess to finish, any timeouts stuff are related to this?

    Edit* no Mandes i haven't tried it, ill try it now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •