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.

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

// 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;

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.