Jump to content

test for valid URL format of string


jasonc

Recommended Posts

i just tried the

 

<?php
$url = 'http://site.com/test/test.html';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);
?>

 

shows the HOST as www.site.com

but if they were to just put say...

 

site.com/test/test.html

 

then HOST is empty and the PATH shows the original string.

 

how can i test to see if any version of the url was entered.

 

if i say what i am wanting to do or prevent this may help a bit more...

 

i wish to prevent a URL web link from being posted in my forum.

 

so if they were to type in say

 

'check this out www.site.com'

 

or

 

'check this out site.com its cool!'

 

or what ever takes their fancy.

 

i have had more recently a few web link to 'buy this software' or the like type of entries and do not wish to be monitoring every single post as this is just not practical with the 100's that are coming in.  and asking visitors to sign up is a no-no as they will just not post the valid posts.

 

just wanting to keep that happy medium and try to automate the site a bit more.

 

 

thinking about this some more and i think this is what i am after..

 

to find out if the string has site.com in it, or what ever the domain is

 

i know this may bring up some false positives but then they can just send them to us via the contact page if needed.

 

 

 

yes this is something like what i am after it checks the whole string, but can this be modified so it check if the string contains a URL

 

'something here site.com  something else here'

 

also if the URL is at the start or and the end not just within the string.

// Checks if string is a URL
// @param string $url
// @return bool
function isURL($url = NULL) {
        if($url==NULL) return false;

        $protocol = '(http://|https://)';
        $allowed = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';

        $regex = "^". $protocol . // must include the protocol
                         '(' . $allowed . '{1,63}\.)+'. // 1 or several sub domains with a max of 63 chars
                         '[a-z]' . '{2,6}'; // followed by a TLD
        if(eregi($regex, $url)==true) return true;
        else return false;

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.