Solved

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

Posted on 2014-01-25
10
450 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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