Jump to content

[SOLVED] how does this site recognize websites?


dadamssg

Recommended Posts

yeah..more than that if youre willing to help though.i just don't really know how to write regex and then how to use preg_replace or whatever i need  :-\

 

k, moving to regex for further help.

Link to comment
Share on other sites

although i'm not that experienced in regex, here an example that you can play with:

<?php
$test = 'http://google.com';
$url = preg_replace('#(?:http:[\/]{2})([a-z0-9-_]+)(.[a-z]{2,})#ui', '<a href="http://$1$2">$1</a>', $test);
echo $url;

 

sadly, it will ignore further extension of the link (eg. http://google.com/i/want/a/cookie , it will only stop at google.com)

 

Then again, you might want to be careful when creating this. For example in jackpf's post, you can add quotes, ampersand, and other symbols that can harm your site, since its doing a no-holds barred any characters may enter

Link to comment
Share on other sites

<?php
function convertLinks($text) {
        $in=array(
        '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si',
        '`((?<!//)(www\.\S+[[:alnum:]]/?))`si'
        );
        $out=array(
        '<a href="$1" >$1</a> ',
        '<a href="http://$1" >$1</a>'
        );
        return preg_replace($in,$out,$text);
    }

$string = "This is some text http://www.google.com and www.google.com and http://google.com but google.com does not work as clickable.";
$string = convertLinks($string);
echo $string;
?>

 

Should work. It is nearly impossible to do the google.com without the www. or http:// so yea. Hopefully that works for you.

 

EDIT:

As an fyi, I did not write that, I found it in the comments at preg_replace I just removed certain portions and renamed it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.