Hi, I need help about preg_match URL. Here is my problem. How can I validate form if a user input this in the form. ex. http://www.domain.com or www.domain.com The form should only accept domain.com. if the http://www and www. are exists in the user's input the php code should output error like "Must not contain http:// and www." if the input is valid script continue. Hope someone could help me about this. Thanks, Highborn
Instead of that, why not just strip it out? if you take out the http:// and www., then it can continue even if they do add it $domain = str_replace(array('http://', 'http://www.'), '', $_POST['domain']); PHP: Not too sure, but it'd remove the http and the http://www, if you only stripped the www. they could have (for example) blawww.com and it'd be removed.
<?php // get host name from URL preg_match('@^(?:http://)?([^/]+)@i', "http://www.php.net/index.html", $matches); $host = $matches[1]; // get last two segments of host name preg_match('/[^.]+\.[^.]+$/', $host, $matches); echo "domain name is: {$matches[0]}\n"; ?> PHP:
if (preg_match('#^http://(?:w{3}\.)?#i', $url)) { echo 'Must not contain http:// and www.'; } else { //valid.... } PHP: