Solved

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

Posted on 2014-01-25
10
448 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 35

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 35

Expert Comment

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

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 110

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 110

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 110

Expert Comment

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

737 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