SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing www.this.com into an actual link

    Hi, this is probably a common question, but all my attempts thus far have failed, and the documentation might as well be in Arabic for what it means to me.

    Anyway, when I pull a news item out of my database, if it finds a URL then I want it to turn it into an actual link.

    For example:

    www.mysite.com becomes <a href="http://www.mysite.com" TARGET="_blank">www.mysite.com</A>

    I have been playing around with str_replace and preg_replace but just can;t get this to work.

    Any advice? Using a system similar to SPF's (i.e. wrapping links in URL tags or something is doable...

    Thanks
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use the [link ] tags than this should work:

    Code:
    $link = "Hello my site is [link ] http://mysite.com[/link ]";
    
    $link = preg_replace("/\[link\](.+?)\[\/link\]/", "<a href='$1' target='_blank'>$1</a>", $link);

    If you don't want to use [link ] maybe this works (I think there was a similar post for this)

    Code:
    $link = preg_replace("/(https?:\/\/)(www)?|(ftp:\/\/)(ftp)?)(.+?)/", "<a href='$1$2$3' target='_blank'>$3</a>", $link);
    If you want to use a [link ] type then maybe it is possible to add a description.

    Code:
    $link = '[link="http://www.domain.com"]description[/link]';
    
    $link = preg_replace("/\[link=\"(.+?)\"\](.+?)\[\/link\]", "<a href='$1' target='_blank'>$2</a>", $link);
    I am not sure if the second one works. However the first and second ones should.
    Last edited by Chris82; May 4, 2002 at 06:46.

  3. #3
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but using the second one, I get this error:

    Code:
    Warning: Compilation failed: unmatched parentheses at offset 36 in C:\apache\htdocs\weirdbeard\blog.php on line 39
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  4. #4
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  5. #5
    ********* Celica Lover Coomer's Avatar
    Join Date
    Apr 2002
    Location
    Not worth the drive
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing wrong with that last code that was posted on that other thread is that if you have http:// or https:// or ftp:// in front of the www.somesite.com part, it won't be included as part of the link. It'll look something like http://www.nissanz.net. I tried to rewrite that code so that it would include the http:// part in the link, but I couldn't get it to work, because then there would be two http:// parts in some urls. Just letting you know so that you don't run into errors.
    + Celica =
    6G Celicas :: My '94-99 Toyota Celica resource

  6. #6
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeh, I realise that. It's no biggy really though
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  7. #7
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I have actually come up with another problem to that code... it doesn't like query strings. It is doing crazy things to any URL that looks like this:

    http://www.somesiteoranother.com/thi...hp?oops=darnit

    Any way to fix it?
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  8. #8
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?
    $text = "http://somesiteoranother.com/thispage.php?oops=darnit, www.somesiteoranother.com/thispage.php?oops=darnit";
    $text = ereg_replace("[www]+.[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $text);
    $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $text);
    echo "$text";
    ?>
    Last edited by redgoals; May 4, 2002 at 17:08.
    http://redgoals.com ... my site ... nuff said

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ereg? NO! :-D

    here's my function again that i posted here. works great, is super fast with the preg functions, and does e-mail addresses, too. if you don't want e-mail addresses done, remove the the first line of the make_clickable() function. usage: `$txt = make_clickable($txt);' :-)

    Code:
    function make_clickable($txt)
    {
    	$txt = preg_replace('#([^\w=+;%?/]|^)(\w+[\w.-]*\w+@\w+[\w.-]*\w+\.[a-z]{2,4})#i', '$1<a href="mailto:$2">$2</a>', $txt);
    
    	$txt = preg_replace('#([^\w/@]|^)((?:www\.[\w-]+\.[\w-]+?\S*?)|(?:[a-z]{3,6}://\S+?))(?=[^a-z0-9/]*?(?:[\s<\][]|(?:&(?:quot|lt|gt);)|$))#ei', 'create_link("$1", "$2")', $txt);
    
    	return $txt;
    }
    
    function create_link($pre, $url)
    {
    	// these 2 lines fix any apostrophes that get
    	// messed up by being passed to the function
    	$pre = str_replace("\\'", "'", $pre);
    	$url = str_replace("\\'", "'", $url);
    	$suf = '';
    
    	if (preg_match('/&amp;amp$/', $url))
    	{
    		$suf = '&amp;amp';
    		$url = substr($url, 0, -4);
    	}
    
    	$html_url = $url;
    
    	if (!preg_match('#^[a-z]{3,6}://#i', $html_url)) { $html_url = "http://$url"; }
    
    	return "$pre<a href=\"$html_url\" target=\"_blank\">$url</a>$suf";
    }
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  10. #10
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works like a charm. Thanks. You wanna explain it?
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  11. #11
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by weirdbeardmt
    You wanna explain it?
    err... no. if you understand regex syntax, you can take the pattern a couple characters at a time and probably be able to figure out what it's looking for. maybe that's what i do when i'm trying to decipher someone else's regular expression.

  12. #12
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by DR_LaRRY_PEpPeR

    if you understand regex syntax
    er, well that's my first problem...
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  13. #13
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the best reference/"tutorial" i can recommend is the PCRE pattern syntax in the PHP manual. it's long, but if you try to study and understand each piece before you move on, i think you can understand a lot better! you can do it.


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
  •