Solved

PHP Validate URL Field. Make sure i contains a certain website URL

Posted on 2014-01-25
10
438 Views
Last Modified: 2014-01-25
I have this validation setup:

if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
  {
  $websiteErr = "Invalid URL";
  }

Open in new window


How do i make sure that url contains lets say "http://facebook.com/" in the url inputed?
0
Comment
Question by:jporter80
  • 4
  • 3
  • 3
10 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39808759
Use strips after you validate the URL format:

if (strpos (strtolower ($url), "facebook.com") !== false)
{
// URL contains facebook.com
}
if you want to make sure its in the domain part of the url, then use parse_url first.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39808761
Sorry, auto correct on my phone changed strpos to strips.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39808809
http://www.damnyouautocorrect.com/

You can do several things depending on what you want to accomplish.  The meaning of "validate" could be pretty broad.  Do you care if there is a valid URL that does not have a corresponding resource on HTTP or FTP?  Do you care if the URL has the wrong protocol? For example, if you have http://facebook.com, it does not point to https://facebook.com and will get rewritten to https://www.facebook.com

You might find this article helpful:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:jporter80
ID: 39808873
It should require http:// or https:// only.  I dont care if has a subdomain or not.. but must require a certain TLD is entered.

Help?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39808935
Do you understand the regular expression you posted?  If not, just tell us in plain language exactly what you want to find in the acceptable URLs and we can probably help from that.
0
 

Author Comment

by:jporter80
ID: 39808954
no not completely.. regular expression always throws me for a loop.  Basically:

Okay Inputs:
http://exactdomain.com/someother stuff
https://exactdomain.com/somother stuff
https://anysubdomain.exactdomain.com/somother stuff
http://anysubdomain.exactdomain.com/somother stuff

Not okay
exactdomain.com/someother stuff
anysubdomain.exactdomain.com/someotherstuff
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39809061
Regular Expressions always throw everybody for a loop!  I find them a little easier to read and understand if I deconstruct them into the components.  See lines 47-85

But even with that: http://xkcd.com/1171/

<?php // RAY_temp_jporter80.php
error_reporting(E_ALL);
echo "<pre>";


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28348079.html
// REF http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html


/* DESCRIPTION OF THE PROBLEM
Okay Inputs:
http://exactdomain.com/someother stuff
https://exactdomain.com/somother stuff
https://anysubdomain.exactdomain.com/somother stuff
http://anysubdomain.exactdomain.com/somother stuff

Not okay
exactdomain.com/someother stuff
anysubdomain.exactdomain.com/someotherstuff
*/


// TEST DATA IS AN ARRAY OF INDIVIDUAL TEST ARRAYS
// KEY = EXPECTED, VALUE = TEST-DATA
$targets
= array

// KNOWN BAD SHOULD NOT MATCH
(  array( "" => "exactdomain.com/someother stuff"
), array( "" => "anysubdomain.exactdomain.com/someotherstuff"

// KNOWN GOOD SHOULD MATCH
), array( "http://exactdomain.com/someother"              => "http://exactdomain.com/someother stuff"
), array( "https://exactdomain.com/somother"              => "https://exactdomain.com/somother stuff"
), array( "https://anysubdomain.exactdomain.com/somother" => "https://anysubdomain.exactdomain.com/somother stuff"
), array( "http://anysubdomain.exactdomain.com/somother"  => "http://anysubdomain.exactdomain.com/somother stuff"
)
)
;


// A SIGNAL STRING PREPARED FOR USE IN A REGULAR EXPRESSION
$signal = preg_quote('exactdomain.com');


// A REGEX THAT FINDS URLS AND DOMAIN SUBSTRINGS
$regex
= '#'         // REGEX DELIMITER

. '\b'        // ON WORD BOUNDARY

. '('         // START GROUP
. 'https?'    // HTTP OR HTTPS
. ')'         // END GROUP
. '{1}'       // EXACTLY ONE OF THIS GROUP

. '('         // START GROUP
. '://'       // COLON, SLASH, SLASH
. ')'         // END GROUP
. '{1}'       // EXACTLY ONE OF THIS GROUP

. '('         // START GROUP
. '[A-Z0-9]'  // A SUBDOMAIN
. '+?'        // INDETERMINATE LENGTH
. '\.'        // A DOT (ESCAPED)
. ')'         // END GROUP
. '??'        // ZERO OR ONE OF THIS GROUP, UNGREEDY

. '('         // START GROUP
. $signal     // THE DOMAIN WE WANT TO FIND
. ')'         // END GROUP

. '('         // GROUP
. '/{1}'      // ONE SLASH
. '.*?'       // URL PATHS (NOT WHITE SPACE)
. ')'         // END GROUP

. '('         // GROUP
. '[\S\B]'    // NOT WHITESPACE, NOT WORD BOUNDARY
. '{0,}'      // URL PATH OR NOTHING
. ')'         // END GROUP

. '#'         // REGEX DELIMITER
. 'i'         // CASE-INSENSITIVE
;

// TEST THE DATA STRINGS IN THE SUB-ARRAYS
foreach ($targets as $arr)
{
    foreach ($arr as $expected => $target)
    {
        preg_match_all($regex, $target, $match);

        // SHOW WHAT HAPPENED
        foreach ($match[0] as $matched)
        {
            // NO OUTPUT IF THE TEST WORKED AS EXPECTED
            if ($matched == $expected) continue;

            // EXPOSITION IF THE TEST DID NOT WORK AS EXPECTED
            echo PHP_EOL;
            echo "<b>EXPECT:</b> $expected";
            echo PHP_EOL;
            echo "<b>INPUTS:</b> $target";
            echo PHP_EOL;
            echo "<b>REGEXP:</b> $regex";
            echo PHP_EOL;
            echo "<b>OUTPUT:</b> ";
            print_r($match[0]);
            echo PHP_EOL;
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:jporter80
ID: 39809138
thanks for the excellent education on regex.. definitely need to practice that.. it can be so helpful.  This worked like a charm
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39809151
Thanks for the points and thanks for using EE, ~Ray
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39809279
While I understand that regex might handle validation for you, you should really consider using parse_url if you want to examine different parts of a URL:

http://us3.php.net/parse_url

It -is- meant for that kind of thing.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question