Jump to content

Checking if correct url?


tiki

Recommended Posts

This is supposed to check if the website is valid and has http://.
If it does not match it echos an error, but it doesnt seem to work correctly.

[code]
<?php
if (!eregi("^((http|https|ftp)://)?([[:alnum:]-])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_.~?-]*)$", stripslashes(trim($_POST["website"])))) {
?>
[/code]
Link to comment
Share on other sites

  • 2 weeks later...
[quote author=tiki link=topic=96937.msg391830#msg391830 date=1152093830]
Anyone?
[/quote]

Well, I'm mostly acquainted with Perl's regular expressions, but it looks like you're trying to match a possible URI followed by some alphanumeric characters (or dashes) followed by a dot followed by 2-4 more alphanumeric characters, then any number of alphanumeric characters plus plenty of symbols, am I right?

So something like "http://phpfreaks.com/" would match while "www.phpfreaks.com" would not.  Nor would an IP address, "http://66.97.171.5/forums/index.php"

I don't think your regular expression is very robust in terms of encompassing all possible valid forms of website addresses.

You might also consider using one of PHP's filesystem functions or network functions to attempt to access the page the user has specified in order to confirm its existence.  I don't know if that would work all the time, but it's a thought.
Link to comment
Share on other sites

  • 2 weeks later...
your pattern uses the ? quantifier after the optional http|https|ftp set.  '?' specifies EITHER 0 or 1 instances of the sub-pattern, so a string with 0 instances of that sub-pattern will be allowed through.

you'll want the 1 or more quantifier, which i can't remember off the top of my head.  have a look in the php manual at syntax.

[b]EDIT:  or do what effigy said.[/b]
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.