How to validate domain name in PHP? - php

Is it possible without using regular expression?
For example, I want to check that a string is a valid domain:
domain-name
abcd
example
Are valid domains. These are invalid of course:
domaia#name
ab$%cd
And so on. So basically it should start with an alphanumeric character, then there may be more alnum characters plus also a hyphen. And it must end with an alnum character, too.
If it's not possible, could you suggest me a regexp pattern to do this?
EDIT:
Why doesn't this work? Am I using preg_match incorrectly?
$domain = '#djkal';
$regexp = '/^[a-zA-Z0-9][a-zA-Z0-9\-\_]+[a-zA-Z0-9]$/';
if (false === preg_match($regexp, $domain)) {
throw new Exception('Domain invalid');
}

<?php
function is_valid_domain_name($domain_name)
{
return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domain_name) //valid chars check
&& preg_match("/^.{1,253}$/", $domain_name) //overall length check
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name) ); //length of each label
}
?>
Test cases:
is_valid_domain_name? [a] Y
is_valid_domain_name? [0] Y
is_valid_domain_name? [a.b] Y
is_valid_domain_name? [localhost] Y
is_valid_domain_name? [google.com] Y
is_valid_domain_name? [news.google.co.uk] Y
is_valid_domain_name? [xn--fsqu00a.xn--0zwm56d] Y
is_valid_domain_name? [goo gle.com] N
is_valid_domain_name? [google..com] N
is_valid_domain_name? [google.com ] N
is_valid_domain_name? [google-.com] N
is_valid_domain_name? [.google.com] N
is_valid_domain_name? [<script] N
is_valid_domain_name? [alert(] N
is_valid_domain_name? [.] N
is_valid_domain_name? [..] N
is_valid_domain_name? [ ] N
is_valid_domain_name? [-] N
is_valid_domain_name? [] N

With this you will not only be checking if the domain has a valid format, but also if it is active / has an IP address assigned to it.
$domain = "stackoverflow.com";
if(filter_var(gethostbyname($domain), FILTER_VALIDATE_IP))
{
return TRUE;
}
Note that this method requires the DNS entries to be active so if you require a domain string to be validated without being in the DNS use the regular expression method given by velcrow above.
Also this function is not intended to validate a URL string use FILTER_VALIDATE_URL for that. We do not use FILTER_VALIDATE_URL for a domain because a domain string is not a valid URL.

PHP 7
// Validate a domain name
var_dump(filter_var('mandrill._domainkey.mailchimp.com', FILTER_VALIDATE_DOMAIN));
# string(33) "mandrill._domainkey.mailchimp.com"
// Validate an hostname (here, the underscore is invalid)
var_dump(filter_var('mandrill._domainkey.mailchimp.com', FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME));
# bool(false)
It is not documented here: http://www.php.net/filter.filters.validate and a bug request for this is located here: https://bugs.php.net/bug.php?id=72013

use checkdnsrr http://php.net/manual/en/function.checkdnsrr.php
$domain = "stackoverflow.com";
checkdnsrr($domain , "A");
//returns true if has a dns A record, false otherwise

Firstly, you should clarify whether you mean:
individual domain name labels
entire domain names (i.e. multiple dot-separate labels)
host names
The reason the distinction is necessary is that a label can technically include any characters, including the NUL, # and '.' characters. DNS is 8-bit capable and it's perfectly possible to have a zone file containing an entry reading "an\0odd\.l#bel". It's not recommended of course, not least because people would have difficulty telling a dot inside a label from those separating labels, but it is legal.
However, URLs require a host name in them, and those are governed by RFCs 952 and 1123. Valid host names are a subset of domain names. Specifically only letters, digits and hyphen are allowed. Furthermore the first and last characters cannot be a hyphen. RFC 952 didn't permit a number for the first character, but RFC 1123 subsequently relaxed that.
Hence:
a - valid
0 - valid
a- - invalid
a-b - valid
xn--dasdkhfsd - valid (punycode encoding of an IDN)
Off the top of my head I don't think it's possible to invalidate the a- example with a single simple regexp. The best I can come up with to check a single host label is:
if (preg_match('/^[a-z\d][a-z\d-]{0,62}$/i', $label) &&
!preg_match('/-$/', $label))
{
# label is legal within a hostname
}
To further complicate matters, some domain name entries (typically SRV records) use labels prefixed with an underscore, e.g. _sip._udp.example.com. These are not host names, but are legal domain names.

Here is another way without regex.
$myUrl = "http://www.domain.com/link.php";
$myParsedURL = parse_url($myUrl);
$myDomainName= $myParsedURL['host'];
$ipAddress = gethostbyname($myDomainName);
if($ipAddress == $myDomainName)
{
echo "There is no url";
}
else
{
echo "url found";
}

I think once you have isolated the domain name, say, using Erklan's idea:
$myUrl = "http://www.domain.com/link.php";
$myParsedURL = parse_url($myUrl);
$myDomainName= $myParsedURL['host'];
you could use :
if( false === filter_var( $myDomainName, FILTER_VALIDATE_URL ) ) {
// failed test
}
PHP5s Filter functions are for just such a purpose I would have thought.
It does not strictly answer your question as it does not use Regex, I realise.

Regular expression is the most effective way of checking for a domain validation. If you're dead set on not using a Regular Expression (which IMO is stupid), then you could split each part of a domain:
www. / sub-domain
domain name
.extension
You would then have to check each character in some sort of a loop to see that it matches a valid domain.
Like I said, it's much more effective to use a regular expression.

Your regular expression is fine, but you're not using preg_match right. It returns an int (0 or 1), not a boolean. Just write if(!preg_match($regex, $string)) { ... }

If you don't want to use regular expressions, you can try this:
$str = 'domain-name';
if (ctype_alnum(str_replace('-', '', $str)) && $str[0] != '-' && $str[strlen($str) - 1] != '-') {
echo "Valid domain\n";
} else {
echo "Invalid domain\n";
}
but as said regexp are the best tool for this.

If you want to check whether a particular domain name or ip address exists or not, you can also use checkdnsrr
Here is the doc http://php.net/manual/en/function.checkdnsrr.php

A valid domain is for me something I'm able to register or at least something that looks like I could register it. This is the reason why I like to separate this from "localhost"-names.
And finally I was interested in the main question if avoiding Regex would be faster and this is my result:
<?php
function filter_hostname($name, $domain_only=false) {
// entire hostname has a maximum of 253 ASCII characters
if (!($len = strlen($name)) || $len > 253
// .example.org and localhost- are not allowed
|| $name[0] == '.' || $name[0] == '-' || $name[ $len - 1 ] == '.' || $name[ $len - 1 ] == '-'
// a.de is the shortest possible domain name and needs one dot
|| ($domain_only && ($len < 4 || strpos($name, '.') === false))
// several combinations are not allowed
|| strpos($name, '..') !== false
|| strpos($name, '.-') !== false
|| strpos($name, '-.') !== false
// only letters, numbers, dot and hypen are allowed
/*
// a little bit slower
|| !ctype_alnum(str_replace(array('-', '.'), '', $name))
*/
|| preg_match('/[^a-z\d.-]/i', $name)
) {
return false;
}
// each label may contain up to 63 characters
$offset = 0;
while (($pos = strpos($name, '.', $offset)) !== false) {
if ($pos - $offset > 63) {
return false;
}
$offset = $pos + 1;
}
return $name;
}
?>
Benchmark results compared with velcrow 's function and 10000 iterations (complete results contains many code variants. It was interesting to find the fastest.):
filter_hostname($domain);// $domains: 0.43556308746338 $real_world: 0.33749794960022
is_valid_domain_name($domain);// $domains: 0.81832790374756 $real_world: 0.32248711585999
$real_world did not contain extreme long domain names to produce better results. And now I can answer your question: With the usage of ctype_alnum() it would be possible to realize it without regex, but as preg_match() was faster I would prefer that.
If you don't like the fact that "local.host" is a valid domain name use this function instead that valids against a public tld list. Maybe someone finds the time to combine both.

The correct answer is that you don't ... you let a unit tested tool do the work for you:
// return '' if host invalid --
private function setHostname($host = '')
{
$ret = (!empty($host)) ? $host : '';
if(filter_var('http://'.$ret.'/', FILTER_VALIDATE_URL) === false) {
$ret = '';
}
return $ret;
}
further reading :https://www.w3schools.com/php/filter_validate_url.asp

If you can run shell commands, following is the best way to determine if a domain is registered.
This function returns false, if domain name isn't registered else returns domain name.
function get_domain_name($domain) {
//Step 1 - Return false if any shell sensitive chars or space/tab were found
if(escapeshellcmd($domain)!=$domain || count(explode(".", $domain))<2 || preg_match("/[\s\t]/", $domain)) {
return false;
}
//Step 2 - Get the root domain in-case of subdomain
$domain = (count(explode(".", $domain))>2 ? strtolower(explode(".", $domain)[count(explode(".", $domain))-2].".".explode(".", $domain)[count(explode(".", $domain))-1]) : strtolower($domain));
//Step 3 - Run shell command 'dig' to get SOA servers for the domain extension
$ns = shell_exec(escapeshellcmd("dig +short SOA ".escapeshellarg(explode(".", $domain)[count(explode(".", $domain))-1])));
//Step 4 - Return false if invalid extension (returns NULL), or take the first server address out of output
if($ns===NULL) {
return false;
}
$ns = (((preg_split('/\s+/', $ns)[0])[strlen(preg_split('/\s+/', $ns)[0])-1]==".") ? substr(preg_split('/\s+/', $ns)[0], 0, strlen(preg_split('/\s+/', $ns)[0])-1) : preg_split('/\s+/', $ns)[0]);
//Step 5 - Run another dig using the obtained address for our domain, and return false if returned NULL else return the domain name. This assumes an authoritative NS is assigned when a domain is registered, can be improved to filter more accurately.
$ans = shell_exec(escapeshellcmd("dig +noall +authority ".escapeshellarg("#".$ns)." ".escapeshellarg($domain)));
return (($ans===NULL) ? false : ((strpos($ans, $ns)>-1) ? false : $domain));
}
Pros
Works on any domain, while php dns functions may fail on some domains. (my .pro domain failed on php dns)
Works on fresh domains without any dns (like A) records
Unicode friendly
Cons
Usage of shell execution, probably

<?php
if(is_valid_domain('https://www.google.com')==1){
echo 'Valid';
}else{
echo 'InValid';
}
function is_valid_domain($url){
$validation = FALSE;
/*Parse URL*/
$urlparts = parse_url(filter_var($url, FILTER_SANITIZE_URL));
/*Check host exist else path assign to host*/
if(!isset($urlparts['host'])){
$urlparts['host'] = $urlparts['path'];
}
if($urlparts['host']!=''){
/*Add scheme if not found*/ if (!isset($urlparts['scheme'])){
$urlparts['scheme'] = 'http';
}
/*Validation*/
if(checkdnsrr($urlparts['host'], 'A') && in_array($urlparts['scheme'],array('http','https')) && ip2long($urlparts['host']) === FALSE){
$urlparts['host'] = preg_replace('/^www\./', '', $urlparts['host']);
$url = $urlparts['scheme'].'://'.$urlparts['host']. "/";
if (filter_var($url, FILTER_VALIDATE_URL) !== false && #get_headers($url)) {
$validation = TRUE;
}
}
}
return $validation;
}
?>

After reading all the issues with the added functions I decided I need something more accurate.
Here's what I came up with that works for me.
If you need to specifically validate hostnames (they must start and end with an alphanumberic character and contain only alphanumerics and hyphens) this function should be enough.
function is_valid_domain($domain) {
// Check for starting and ending hyphen(s)
if(preg_match('/-./', $domain) || substr($domain, 1) == '-') {
return false;
}
// Detect and convert international UTF-8 domain names to IDNA ASCII form
if(mb_detect_encoding($domain) != "ASCII") {
$idn_dom = idn_to_ascii($domain);
} else {
$idn_dom = $domain;
}
// Validate
if(filter_var($idn_dom, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) != false) {
return true;
}
return false;
}
Note that this function will work on most (haven't tested all languages) LTR languages. It will not work on RTL languages.
is_valid_domain('a'); Y
is_valid_domain('a.b'); Y
is_valid_domain('localhost'); Y
is_valid_domain('google.com'); Y
is_valid_domain('news.google.co.uk'); Y
is_valid_domain('xn--fsqu00a.xn--0zwm56d'); Y
is_valid_domain('area51.com'); Y
is_valid_domain('japanese.コム'); Y
is_valid_domain('домейн.бг'); Y
is_valid_domain('goo gle.com'); N
is_valid_domain('google..com'); N
is_valid_domain('google-.com'); N
is_valid_domain('.google.com'); N
is_valid_domain('<script'); N
is_valid_domain('alert('); N
is_valid_domain('.'); N
is_valid_domain('..'); N
is_valid_domain(' '); N
is_valid_domain('-'); N
is_valid_domain(''); N
is_valid_domain('-günter-.de'); N
is_valid_domain('-günter.de'); N
is_valid_domain('günter-.de'); N
is_valid_domain('sadyasgduysgduysdgyuasdgusydgsyudgsuydgusydgsyudgsuydusdsdsdsaad.com'); N
is_valid_domain('2001:db8::7'); N
is_valid_domain('876-555-4321'); N
is_valid_domain('1-876-555-4321'); N

I know that this is an old question, but it was the first answer on a Google search, so it seems relevant. I recently had this same problem. The solution in my case was to just use the Public Suffix List:
https://publicsuffix.org/learn/
The suggested language specific libraries listed should all allow for easy validation of not just domain format, but also top level domain validity.

Check the php function checkdnsrr
function validate_email($email){
$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*#([a-z0-9]+([._-][a-z0-9]+))+$";
if(eregi($exp,$email)){
if(checkdnsrr(array_pop(explode("#",$email)),"MX")){
return true;
}else{
return false;
}
}else{
return false;
}
}

This is validation of domain name in javascript:
<script>
function frmValidate() {
var val=document.frmDomin.name.value;
if (/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/.test(val)){
alert("Valid Domain Name");
return true;
} else {
alert("Enter Valid Domain Name");
val.name.focus();
return false;
}
}
</script>

This is simple. Some php egnine has a problem with split().
This code below will work.
<?php
$email = "vladimiroliva#ymail.com";
$domain = strtok($email, "#");
$domain = strtok("#");
if (#getmxrr($domain,$mxrecords))
echo "This ". $domain." EXIST!";
else
echo "This ". $domain." does not exist!";
?>

Related

Function for normalizing Canadian postal codes is returning unexpected results

I'm trying to normalize user input for Canadian postal codes.
The end goal is to get all input as A3A 3A3 - 7 characters, capitalized, with the first group letter number letter, second group as number letter number.
Users will sometimes use o or O for the number 0, or vice versa, so here is my function:
$zip = htmlspecialchars($_REQUEST['zip']);
if (!empty($zip)) {
//if the format is A3A3A3, split it into two groups
if (strlen($zip) == 6) {
$zip = substr($zip, 0, 3) . " " . substr($zip, 3, 3);
}
//now lets make sure there is only a empty space between groups
if (strlen($zip) == 7) {
$zip[3] = " ";
}
//let's put their o's, O's and 0's in the right places
if ($zip[1] == ("O" || "o")) {
$zip[1] = '0';
}
if ($zip[4] == ('O'||'o')) {
$zip[4] = '0';
}
if ($zip[6] == ('O'||'o')) {
$zip[6] = '0';
}
if ($zip[0] == '0') {
$zip[0] = 'O';
}
if ($zip[2] == '0') {
$zip[2] = 'O';
}
if ($zip[5] == '0') {
$zip[5] = 'O';
}
//uppercase all the letters
strtoupper($zip);
}
The first tests I ran were of the sort:
input[0o0 O0o] => output[O0O 0O0]
Everything looks good I said to myself, it's working! But then I tried a normal postal code and keep getting unexpected results:
input[S7K3K1] => output[S0K 0K0]
I can't figure out why the code is deciding that ($zip[1] == ('O' || 'o') is true when $zip[1] == 7.
Any input would be greatly appreciated. Also, if you see a better way of normalizing this input, please share!
//let's put their o's, O's and 0's in the right places
if ($zip[1] == ("O" || "o")) {
$zip[1] = '0';
}
This code does not do what you think – your comparison logic are wrong, because so is your understanding of the || operator.
Use console.log(("O" || "o")) and see what you get in console – ooops, that’s just the letter O. Why? Because the || logical or operator in JS returns the first value that is not false-y.
You need to either write two individual comparisons here, and or-conjoin those – $zip[1] == "O" || $zip[1] == "o", or use an array with possible values and then check whether your variable value is among those (but the latter would be a bit over the top here).
Edit: Or, as bigmandan pointed out, you could also transform your letter to upper- or lower-case first, then you only have to do one comparison: ($zip[1]).toLowerCase() == "o" (Of course this works only in a special case like this – if it was not O or o, but a or b instead, you would still have to do individual comparisons.)

Change IPv4 to IPv6 string

Sander Steffann mentioned in a previous question of mine:
Addresses like 0000:0000:0000:0000:0000:0000:192.168.0.1 are written as
0000:0000:0000:0000:0000:0000:c0a8:0001 which is exactly the same address
but in hex notation.
How do I detect in PHP if an address was written like eg.: ::0000:192.168.0.1 or 0000::0000:192.168.0.1 or 0000:0000:0000:0000:0000:0000:192.168.0.1 etc.? Is it enough to check if an IP-based string has '.' AND ':' ?
And how do I change this to the full string 0000:0000:0000:0000:0000:0000:c0a8:0001?
Am I correct, to change this to IPv4 will be something like:
<?php
$strIP = '0000:0000:0000:0000:0000:0000:192.168.0.1';
$strResult = substr($strIP, strrpos($strIP, ':'));
echo $strResult; //192.168.0.1 ?
?>
... or are correct IP string representations more complex than what this snippet could do?
I can't believe I wrote this all out in one go and it worked the first time.
$strIP = '0000:0000:0000:0000:0000:0000:192.168.0.1';
$arrIP = explode(':', $strIP);
if( preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $arrIP[count($arrIP)-1]) ) {
$ip4parts = explode('.', $arrIP[count($arrIP)-1]);
$ip6trans = sprintf("%02x%02x:%02x%02x", $ip4parts[0], $ip4parts[1], $ip4parts[2], $ip4parts[3]);
$arrIP[count($arrIP)-1] = $ip6trans;
$strIP = implode(':', $arrIP);
}
echo $strIP; //output: 0000:0000:0000:0000:0000:0000:c0a8:0001
Basically:
Explode the string on :
Check if the last quad is formatted like an IP4 address
Explode the last quad on .
Re-print the IP4 octets into two hex quads
Replace the IP4 quad with the new ones
Implode the array on :.
Your best bet is to not do this manually, but instead call inet_pton to get a binary representation, and then convert that to the format you wish to have.
$foo = inet_pton("::1");
for ($i = 0 ; $i < 8 ; $i++)
$arr[$i] = sprintf("%02x%02x", ord($foo[$i * 2]), ord($foo[$i * 2 + 1]));
$addr = implode(":", $arr);
First of all: why would you care how the address is written? inet_pton() will parse all variations for you and give you a consistent result, which you can then transform into binary, hex, or whatever you want.
All the code for converting things like ::192.168.0.1 to 0000:0000:0000:0000:0000:0000:c0a8:0001 was actually in my post. That's exactly what my example function does.
If you feed 0000:0000:0000:0000:0000:0000:192.168.0.1 to inet_pton() and then to inet_ntop() you'll get the canonical IPv6 notation, which is ::192.168.0.1 in this case. If that string begins with :: and the rest contains no : and three dots then you can be pretty sure it's an IPv4 address ;-)
To combine the answer to your previous question with this question:
function expand_ip_address($addr_str) {
/* First convert to binary, which also does syntax checking */
$addr_bin = #inet_pton($addr_str);
if ($addr_bin === FALSE) {
return FALSE;
}
$addr_hex = bin2hex($addr_bin);
/* See if this is an IPv4-Compatible IPv6 address (deprecated) or an
IPv4-Mapped IPv6 Address (used when IPv4 connections are mapped to
an IPv6 sockets and convert it to a normal IPv4 address */
if (strlen($addr_bin) == 16
&& substr($addr_hex, 0, 20) == str_repeat('0', 20)) {
/* First 80 bits are zero: now see if bits 81-96 are either all 0 or all 1 */
if (substr($addr_hex, 20, 4) == '0000')
|| substr($addr_hex, 20, 4) == 'ffff')) {
/* Remove leading bits so only the IPv4 bits remain */
$addr_bin = substr($addr_hex, 12);
}
}
/* Then differentiate between IPv4 and IPv6 */
if (strlen($addr_bin) == 4) {
/* IPv4: print each byte as 3 digits and add dots between them */
$ipv4_bytes = str_split($addr_bin);
$ipv4_ints = array_map('ord', $ipv4_bytes);
return vsprintf('%03d.%03d.%03d.%03d', $ipv4_ints);
} else {
/* IPv6: print as hex and add colons between each group of 4 hex digits */
return implode(':', str_split($addr_hex, 4));
}
}

Refactoring PHP-based IP-Filter to work with IPv6

I have an IP-Filter for a whitelist in one of my older projects, which I want to reuse in a new application.
edit for clarification; It works like this:
The whitelist contains entries in a format specified below. Using foreach ($whitelist as $listed), I check which type the current entry ($listed) is, and then compare this entry with $ip. As soon as it finds an entry, that matches the specified IP, it will return true, if after going through the whole whitelist no match was found it will return false.
As of now, only IPv4 is supported and the filter allows for a whitelist entries like followed:
IP-range by specifying BEGIN - END (192.168.0.1-192.168.0.5)
single IP-address (e.g. 192.168.0.2)
IP-range using a *-wildcard (e.g. 192.168.0.*)
The methods to check each of these cases look like this, where $ip is the client's IP and $listed is an entry from the whitelist/blacklist matching one of the above mentioned formats:
public function checkAgainstRange($ip, $listed)
{
list($begin, $end) = explode('-', $listed);
$begin = ip2long($begin);
$end = ip2long($end);
$ip = ip2long($ip);
return ($ip >= $begin && $ip <= $end);
}
public function checkAgainstSingle($ip, $listed)
{
return long2ip(ip2long($ip)) === long2ip(ip2long($listed));
}
public function checkAgainstWildcard($ip, $listed)
{
$listedSegment = explode('.', $listed);
$ipSegment = explode('.', $ip);
for ($i = 0; $i < count($listedSegment); $i++) {
// We're finished when the wildcarded block is reached
// Important: Check for wildcard first, as it won't match the client IP!
if ($listedSegment[$i] == '*') {
return true;
}
if ($listedSegment[$i] != $ipSegment[$i]) {
return false;
}
}
// Just to be safe: If we reach this, something went wrong
return false;
}
I need some directions as to how to make these work with IPv6-addresses.
Some of the required changes are obvious:
* ip2long() only works with IPv4-addresses
* I have to check for : as a possible delimiter in checkAgainstWildcard().
I found inet_ntop()and inet_pton() in the php-docs. Can I just use the following to compare two single ip-addresses?
public function checkAgainstSingle($ip, $listed)
{
$ip = inet_ntop($ip);
$listed = inet_ntop($listed);
if ($ip === false || $listed === false) {
throw new \Exception;
}
return $ip === $false;
}
Usage examples:
$filter = new IpFilter();
$ip = $_SERVER['REMOTE_ADDR'];
$result = $filter->checkAgainstSingle($ip, '192.168.0.1');
$result = $filter->checkAgainstRange($ip, '192.168.0.1-192.168.0.10');
$result = $filter->checkAgainstWildcard($ip, '192.168.0.*');
Is it even useful to keep something like checkAgainstRange()? And if so, how could I even check for IPv6-ranges in a similar way? Obviously I cant't change ip2long() to inet_ntop() here...
Same goes for wildcard-ranges. Should I keep them and would it suffice to check for : as a segment delimiter and if it is not found, fall back to . as delimiter?
If the IP address in in human-readable/printable format then you can use inet_pton() to convert it to binary form. Then you can for example use bin2hex() to show the binary data in hexadecimal form.
Example:
$address = inet_pton("2a00:8640:1::1");
echo bin2hex($address);
will show you:
2a008640000100000000000000000001
If you then want to check on subnet instead of on separate address you can compare the first 16 hexadecimal characters. IPv6 subnets are almost always /64 networks, each hexadecimal character is 4 bits, so the first (64 / 4 =) 16 characters show you the subnet. Remember that with IPv6 a machine can have multiple IPv6 addresses and if it uses privacy extensions (enabled by default on recent Windows and Mac OS X systems) then it will change it's source address regularly. Matching on subnet might be the most useful for IPv6.

Regular Expression (avoid float numbers)

I want a pattern to create a "is_id()" function to validate user input before mysql query. The pattern most contain ONLY numbers, my problem is avoid the float numbers:
function is_id($id) {
$pattern = "/^[0-9]+/";
if(preg_match($pattern,$id)) {
echo "ok";
} else {
echo "error";
}
}
is_id(0) // error
is_id(-5) // error
is_id(-5.5) // error
is_id(1.5) // ok <-- THIS IS THE PROBLEM
is_id(10) // ok
is_id("5") // ok
is_id("string") // error
$ denotes the end of a line/string to match.
/^[0-9]+$/
You're missing the trailing $ in your pattern. In is_id(1.5) your pattern is matching the 1 and stopping. If you add a trailing $ (as in ^[0-9]+$) then the pattern will need to match the entire input to succeed.
Why use a regex? Why not check types (this isn't as tiny as the regex, but it may be more semantically appropriate)
function is_id($n) {
return is_numeric($n) && floor($n) == $n && $n > 0;
}
is_numeric() verifies that it's either a float, an int, or a number than can be converted.
floor($n) == $n checks to see if it's indeed an integer.
$n > 0 checks to see if it's greater than 0.
Done...
You don't need regex for this, you can use a simple check like so:
function is_id($id)
{
return ((is_numeric($id) || is_int($id)) && !is_float($id)) && $id > -1
}
The output is as follows:
var_dump(is_id(0)); // false - are we indexing from 0 or 1 ?
var_dump(is_id(-5)); // false
var_dump(is_id(-5.5)); // false
var_dump(is_id(1.5)); // false
var_dump(is_id(10)); // true
var_dump(is_id("5")); // true
var_dump(is_id("string")); // false
I favour ircmaxell's answer.

looking to validate US phone number w/ area code

I'm working on a function to validate a US phone number submitted by a user, which can be submitted in any of the popular number formats people usually use. My code so far is as follows:
$number = '123-456-7890';
function validate_telephone_number($number) {
$formats = array(
'###-###-####',
'(###)###-###',
'(###) ###-###',
'##########'
);
$number = trim(preg_replace('[0-9]', '#', $number));
if (in_array($number, $formats)) {
return true;
} else {
return false;
}
}
First off, this code does not seem to be working, and returns false on all submitted numbers. I can't seem to find my error.
Secondly, I'm looking for an easy way to only allow phone numbers from an array of specific allowed area codes. Any ideas?
For your first question:
preg_replace('/[0-9]/', '#', $number)
or '/\d/'
For the second question this may help you:
$areaCode = substr(preg_replace('/[^\d]/', '', $number),0 , 3);
This will give you the first 3 digits in the number by discarding all other characters.
I'm not familiar with the US area codes format so I cannot help you more with this one.
Bonus:
if (in_array($number, $formats)) {
return true;
} else {
return false;
}
is equivalent to
return in_array($number, $formats);
As a matter of fact any statement of the form
if(<expression>){
return true;
}
else{
return false;
}
can be written as return (bool) <expr>;, but in this case in_array will always return a Boolean so (bool) is not needed.
Your code does not check for well formatted but invalid numbers - for example, no area code starts with 0 or 1 in the US, so this could be checked. Also, your formats do not allow for country code inclusion - +15551234567 would be rejected, for example.
If you don't care about the formatting and just want to validate if the digits in the input amount to a valid US phone number, you could use something like this:
$clean_number = preg_replace("/[^0-9]/", '', $number);
$valid = preg_match("/^(\+?1)?[2-9][0-9]{9}$/", $clean_number);
Of course, this will also accept "foo 5555555555 bar" as a valid number - if you want to disallow that, make the preg_replace more restrictive (e.g, remove only brackets, spaces and dashes).
If you prefer to do this without maintaining a lot of code, you an check out this API that validates a US number and provides several formats for the number https://www.mashape.com/parsify/format
Look here for a code project that has a function for validating phone numbers.

Categories