Jump to content

[SOLVED] how does this site recognize websites?


dadamssg

Recommended Posts

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

<?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.

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.