Jump to content

Check wildcard URL


rhyspaterson

Recommended Posts

Hey guys,

 

I'm looking for a function that will help me determine whether or not a URL entered by a user matches specific requirements. This is done using $_POST.

 

Basically the format of the URL must be either: www.example.com, *.example.com, www.example.*, or *.example.* - or some kind of combination such as www.url.com*. I don't want to allow a star in the 'url'.

 

So in theory, to check this, all i would have to do is make sure the string contains:

 

[www and/or *].[text].[text and/or *]

 

I think my theory is correct..? Could anyone please help me out with this function?

 

Thanks heaps lads :D

Link to comment
https://forums.phpfreaks.com/topic/58494-check-wildcard-url/
Share on other sites

Using regex should work?

 

$url = $_POST['url']; // do stripslashes and other cleaning here probably

if (preg_match('/(www\.example\.com|www\.example\..+|.+\.example\..+)/i',$url))
{
  // It matched
}

 

in regex . means match anything, the + is a quantifier of 1 or more, stuff inside parenthesis separated by pipe is basically "or"  (a|b|c).. special chars like . and / needs to be escaped with \, so a \. means literal .

 

regex can be complex stuff but for this task it should be pretty simple..

 

Link to comment
https://forums.phpfreaks.com/topic/58494-check-wildcard-url/#findComment-290092
Share on other sites

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.