Unpaired t-test in PHP - php
Is there a function, or example code, to do an unpaired t-test in PHP? I see there is a PHP function for a paired t-test (http://php.net/manual/en/function.stats-stat-paired-t.php) but I'm looking for the unpaired variety.
I found a PHP function (http://php.net/manual/en/function.stats-stat-independent-t.php) but its not documented and I'm not sure what value it is returning. So I created my own function, which returns more information. Hopefully this will be helpful to someone in the future:
/* for calculating t-test's critical value */
$critval = array(0,6.3138,2.92,2.3534,2.1319,2.015,1.9432,1.8946,1.8595,1.8331,1.8124,1.7959,1.7823,1.7709,1.7613,1.753,1.7459,1.7396,1.7341,1.7291,1.7247,1.7207,1.7172,1.7139,1.7109,1.7081,1.7056,1.7033,1.7011,1.6991,1.6973,1.6955,1.6939,1.6924,1.6909,1.6896,1.6883,1.6871,1.6859,1.6849,1.6839,1.6829,1.682,1.6811,1.6802,1.6794,1.6787,1.6779,1.6772,1.6766,1.6759,1.6753,1.6747,1.6741,1.6736,1.673,1.6725,1.672,1.6715,1.6711,1.6706,1.6702,1.6698,1.6694,1.669,1.6686,1.6683,1.6679,1.6676,1.6673,1.6669,1.6666,1.6663,1.666,1.6657,1.6654,1.6652,1.6649,1.6646,1.6644,1.6641,1.6639,1.6636,1.6634,1.6632,1.663,1.6628,1.6626,1.6623,1.6622,1.662,1.6618,1.6616,1.6614,1.6612,1.661,1.6609,1.6607,1.6606,1.6604,1.6602,1.6601,1.6599,1.6598,1.6596,1.6595,1.6593,1.6592,1.6591,1.6589,1.6588,1.6587,1.6586,1.6585,1.6583,1.6582,1.6581,1.658,1.6579,1.6578,1.6577,1.6575,1.6574,1.6573,1.6572,1.6571,1.657,1.657,1.6568,1.6568,1.6567,1.6566,1.6565,1.6564,1.6563,1.6562,1.6561,1.6561,1.656,1.6559,1.6558,1.6557,1.6557,1.6556,1.6555,1.6554,1.6554,1.6553,1.6552,1.6551,1.6551,1.655,1.6549,1.6549,1.6548,1.6547,1.6547,1.6546,1.6546,1.6545,1.6544,1.6544,1.6543,1.6543,1.6542,1.6542,1.6541,1.654,1.654,1.6539,1.6539,1.6538,1.6537,1.6537,1.6537,1.6536,1.6536,1.6535,1.6535,1.6534,1.6534,1.6533,1.6533,1.6532,1.6532,1.6531,1.6531,1.6531,1.653,1.6529,1.6529,1.6529,1.6528,1.6528,1.6528,1.6527,1.6527,1.6526,1.6526,1.6525,1.6525);
/* -------------------------------------------- */
/* ------- sd_square -------------------------- */
/* -------------------------------------------- */
// Function to calculate square of value - mean
function sd_square($x, $mean) { return pow($x - $mean,2); }
/* -------------------------------------------- */
/* ------- sd --------------------------------- */
/* -------------------------------------------- */
// Function to calculate standard deviation (uses sd_square)
function sd($array) {
// square root of sum of squares devided by N-1
return sqrt(array_sum(array_map("sd_square", $array, array_fill(0,count($array), (array_sum($array) / count($array)) ) ) ) / (count($array)-1) );
}
/* -------------------------------------------- */
/* ------- unpairedttest ---------------------- */
/* -------------------------------------------- */
function unpairedttest($data1,$data2) {
if ((count($data1) < 2) || (count($data2) < 2)) {
return array(0,0,0,0,0);
}
/* compute stats about dataset1 */
$mean1 = array_sum($data1)/count($data1);
$sd1 = sd($data1);
$variance1 = $sd1*$sd1;
$n1 = count($data1);
$stderr1 = $variance1/$n1;
/* compute stats about dataset2 */
$mean2 = array_sum($data2)/count($data2);
$sd2 = sd($data2);
$variance2 = $sd2*$sd2;
$n2 = count($data2);
$stderr2 = $variance2/$n2;
$stderr = sqrt($stderr1 + $stderr2);
$meandiff = abs($mean1-$mean2);
if ($stderr > 0) {
$tvalue = $meandiff/$stderr;
}
else {
$tvalue = 0;
}
$df = $n1 + $n2 -2;
if ($df > 100) {
$criticaltvalue = $GLOBALS['critval'][100];
}
else {
$criticaltvalue = $GLOBALS['critval'][$df];
}
if ($tvalue > $criticaltvalue) {
/* they are statistical different */
$statisticallydifferent = 1;
}
else {
/* they ain't */
$statisticallydifferent = 0;
}
return array($statisticallydifferent,$stderr,$meandiff,$tvalue,$df);
}
Related
PHP - Location inside multiple polygons
How can i turn this in to supporting more polygons than just this one? I think of something with arrays but I’m lost this code works with one polygon but just can’t get any further this is what I got: I would like it to check if the point is in polygon A or B or C and so on depending on how many polygons I got right now it only checking if point is inside polygon A. class Point { public $lat; public $long; function Point($lat, $long) { $this->lat = $lat; $this->long = $long; } } //the Point in Polygon function function pointInPolygon($p, $polygon) { //if you operates with (hundred)thousands of points set_time_limit(60); $c = 0; $p1 = $polygon[0]; $n = count($polygon); for ($i=1; $i<=$n; $i++) { $p2 = $polygon[$i % $n]; if ($p->long > min($p1->long, $p2->long) && $p->long <= max($p1->long, $p2->long) && $p->lat <= max($p1->lat, $p2->lat) && $p1->long != $p2->long) { $xinters = ($p->long - $p1->long) * ($p2->lat - $p1->lat) / ($p2->long - $p1->long) + $p1->lat; if ($p1->lat == $p2->lat || $p->lat <= $xinters) { $c++; } } $p1 = $p2; } // if the number of edges we passed through is even, then it's not in the poly. return $c%2!=0; } $polygon = array( new Point(54.992883, -9.860767), new Point(54.992775, -9.860289), new Point(54.992236,- 9.861030), new Point(54.992473, -9.862007) );
Generate Interpolation Function from points PHP
I need to make a PHP script that generates the interpolation function from the set of points. I have decided to use the Lagrange Interpolation because it was easiest for me to find the example that generates the function from a list of input points. The issues with other methods is that I couldn't find an example that generates the function -> all other examples for all other interpolations only generate additional points and not the function out of the existing points. The source that I've used to find the example for the Lagrange Interpolation is: http://www2.lawrence.edu/fast/GREGGJ/Math420/Section_3_1.pdf I've decided to replicate this example in my PHP code. /** * Generate one basis polynomial function * #param type $points array of points * #param type $basisPolynomial Each basis polynomial will be stored in the array of values so that it can be appended to the final function * #param type $allXValues all x values for the point * #param type $i current index of the basis polynomial */ function generateBasisPolynomial(&$basisPolynomial, $allXValues, $i) { $basisPolynomial[$i] = "("; $divisor = "("; for ($j = 0; $j < count($allXValues); $j++) { if ($j == $i) { continue; } $basisPolynomial[$i] .= "(x-$allXValues[$j])*"; $divisor .="($allXValues[$i]-$allXValues[$j])*"; } //multiply the divisor by 1, because the previous loop has * at the end of the equation $divisor .="1)"; $basisPolynomial[$i] .="1)/$divisor"; } /** * Function that generates the Lagrange interpolation from the list of points * #param type $points * #return string */ function generateLagrangeInterpolation($points) { $numberOfPoints = count($points); if ($numberOfPoints < 2) { return "NaN"; } else { //source http://www2.lawrence.edu/fast/GREGGJ/Math420/Section_3_1.pdf //for each point, construct the basis polynomial //for a sequence of x values, we will have n basis polynomials, //Example: //if we, for example have a sequence of four points, with their sequence of x values being {x0,x1,x2,x3} //then we construct the basis polynomial for x0 by doing the following calculation: //F(x) = ((x-x1)*(x-x2)*(x-x3))/((x0-x1)*(x0-x2)*(x0-x3)) -> where x is an unknown variable. $basisPolynomial = array(); //get all x values from the array of points so that we can access them by index $allXValues = array_keys($points); $allYValues = array_values($points); //Because the Y values are percentages, we want to divide them by 100. $allYValues = array_map(function($val) { return $val / 100; }, $allYValues); $returnFunction = ""; for ($i = 0; $i < $numberOfPoints; $i++) { generateBasisPolynomial($basisPolynomial, $allXValues, $i); //multiply this basis polynomial by y value $returnFunction .="$allYValues[$i]*$basisPolynomial[$i]+"; } //Append 0 to the end of the function because the above loop returns a function with a + //at the end so we want to make it right $returnFunction .="0"; echo $returnFunction; } } //$points = array("4.1168" => "0.213631", "4.19236" => "0.214232", "4.20967" => "0.21441", "4.46908" => "0.218788"); $points = array("0.1" => "5", "0.3" => "10", "0.5" => "30", "0.6" => "60", "0.8" => "70"); generateLagrangeInterpolation($points); What I am getting as a result is the following function: 0.05*((x-0.3)*(x-0.5)*(x-0.6)*(x-0.8)*1)/((0.1-0.3)*(0.1-0.5)*(0.1-0.6)*(0.1-0.8)*1)+0.1*((x-0.1)*(x-0.5)*(x-0.6)*(x-0.8)*1)/((0.3-0.1)*(0.3-0.5)*(0.3-0.6)*(0.3-0.8)*1)+0.3*((x-0.1)*(x-0.3)*(x-0.6)*(x-0.8)*1)/((0.5-0.1)*(0.5-0.3)*(0.5-0.6)*(0.5-0.8)*1)+0.6*((x-0.1)*(x-0.3)*(x-0.5)*(x-0.8)*1)/((0.6-0.1)*(0.6-0.3)*(0.6-0.5)*(0.6-0.8)*1)+0.7*((x-0.1)*(x-0.3)*(x-0.5)*(x-0.6)*1)/((0.8-0.1)*(0.8-0.3)*(0.8-0.5)*(0.8-0.6)*1)+0 I don't care that the expression is simplified and calculated fully (however if you have any advice or code that could do that for me it would be a huge plus). If I look at the simplified expression it looks something like this: (47500*x^4-79300*x^3+42245*x^2-8699*x+480)/(-840) However if I try to paste that function into http://fooplot.com -> I get that the graph is passing through the points defined as the input parameters, however, I'm not sure if the graph for the other points is correct as it looks like it's Y values go into minus values when X <=0 or x>=1. Do you advise that I use the different function or the existing error in the interpolation can be reduced if I had more input points? I have to be honest that I am a poor mathematician so any real example of a more accurate method or example in the code would be greatly appreciated. Thanks
Here's what you can try: function basisPolynomial($points, $j, $x) { $xj = $points[$j][0]; //Assume a point is an array of 2 numbers $partialProduct = 1; //Product loop for ($m = 0;$i < count($points);$m++) { if ($m === $j) { continue; } $partialProduct *= ($x - $points[$m][0])/($xj-$points[$m][0]); } return $partialProduct; } function lagrangePolynomial($points,$x) { $partialSum = 0; for ($j = 0;$j < count($points);$j++) { $partialSum += $points[$j][1]*basisPolynomial($points,$j,$x); } return $partialSum; } Now if you need to plot it you can generate a list of points that can be used in a plotting function e.g. $points = <my points>; $plotPoints = []; for ($i = 0;$i < 10;$i+= 0.1) { //for example $plotPoints[] = [ $i, lagrangePolynomial($points,$i) ]; } If you want to just use the to directly plot you need to use a plotting tool like gnuplot to define the functions and have it determine how to plot them. Update: http://www.physics.brocku.ca/Courses/5P10/lectures/lecture_10_handout.pdf seems to have a gnuplot example of exactly what you need but it feels like cheating of sorts
I'm not much familiarized with the object oriented implementation of php, but in java I do this little baby ;) import java.util.*; import java.util.Arrays; import java.util.List; public class Run{ public static void main(String[] args){ int[] parameters = new int[]{-1, 2, 4, 3}; Binom b = new Binom(1, 1, parameters[1], 0); Polinom p = new Polinom(b); for(int i = 2; i < parameters.length; i++) p.joinBinom(new Binom(1, 1, -1 * parameters[i], 0)); System.out.println(p.toString() + " / (" + getDenominator(parameters) + ")"); } public static int getDenominator(int[] params){ int result = 1; for(int i = 1; i < params.length; i++) result *= params[0] - params[i]; return result; } } class Monomial{ private int constant = 1; private int pow = 0; public int getConstant(){ return this.constant; } public void sumConstant(int value){ this.constant += value; } public boolean hasVariable(){ return this.pow > 0; } public int getPow(){ return this.pow; } public Monomial(int constant, int pow){ this.constant = constant; this.pow = pow; } public ArrayList<Monomial> multiply(Binom a){ Monomial first = new Monomial(this.constant * a.getFirst().getConstant(), this.pow + a.getFirst().getPow()); Monomial second = new Monomial(this.constant * a.getSecond().getConstant(), this.pow + a.getSecond().getPow()); System.out.print("\t" + this.toString() + "\t* (" + a.getFirst().toString() + " " + a.getSecond().toString() + ")"); System.out.print("\t= " + first.toString() + "\t"); System.out.println(second.toString()); return (new Binom(first, second)).toList(); } public String toString(){ String result = ""; if(this.constant == 1){ if(!this.hasVariable()) result += this.constant; } else result += this.constant; if(this.hasVariable()){ result += "X"; if(this.pow > 1) result += "^" + this.pow; } return result; } } class Binom{ private Monomial first; private Monomial second; public Monomial getFirst(){ return this.first; } public Monomial getSecond(){ return this.second; } public Binom(int constant1, int pow1, int constant2, int pow2){ this.first = new Monomial(constant1, pow1); this.second = new Monomial(constant2, pow2); } public Binom(Monomial a, Monomial b){ this.first = a; this.second = b; } public ArrayList<Monomial> toList(){ ArrayList<Monomial> result = new ArrayList<>(); result.add(this.first); result.add(this.second); return result; } } class Polinom{ private ArrayList<Monomial> terms = new ArrayList<>(); public Polinom(Binom b){ this.terms.add(b.getFirst()); this.terms.add(b.getSecond()); } private void compact(){ for(int i = 0; i < this.terms.size(); i++){ Monomial term = this.terms.get(i); for(int j = i + 1; j < this.terms.size(); j++){ Monomial test = this.terms.get(j); if(term.getPow() == test.getPow()){ term.sumConstant(test.getConstant()); this.terms.remove(test); j--; } } } } public void joinBinom(Binom b){ ArrayList<Monomial> result = new ArrayList<>(); for(Monomial t : this.terms){ result.addAll(t.multiply(b)); } this.terms = result; this.compact(); } public String toString(){ String result = ""; for(Monomial t : this.terms) result += (t.getConstant() < 0 ? " " : " +") + t.toString(); return "(" + result + ")"; } } which return: X * (X -4) = X^2 -4X 2 * (X -4) = 2X -8 X^2 * (X -3) = X^3 -3X^2 -2X * (X -3) = -2X^2 6X -8 * (X -3) = -8X 24 ( +X^3 -5X^2 -2X +24) / (-60)
Looks like the current algorithm for Lagrange interpolation method provides the correct results. To correct the errors in calculation, more base points can be provided. Also, to multiply the unknown variables in mathematic function, a function example was left in one of the answers. Thanks everyone.
How to get all php member variables with phpcs
I am trying to get a list of class variables using phpcs but can only get a list of all variables in the file. Has anyone got any ideas on achieving this? Sniff Code for ($i = $stackPtr; $i < count ($tokens); $i++) { $variable = $phpcsFile->findNext(T_VARIABLE, ($i)); if ($variable !== false) { print ($variable); } else { break; } } File to analyse <?PHP /** * #package xxxx * #subpackage Pro/System/Classes * #copyright * #author * #link */ class Test_Class_AAA { const CONSTANT = 'constant value'; public $var1 = 12; const CONSTANT2 = 'constant value 2'; /** * Return record schema details given a LabelIndentifier/LabelNumber or * TransactionCode. * * #param string $type record type * #param string $myextralongparam record type * #return array record schema details (or null) */ private function func ($type,$myextralongparam) { if (true) { $x = 10; $y = 11; $z = $x + $y; $f = $x - $y $f = $x * $t; $f = $x / $y; $f = $x % $y; $f = $x ** $y; $f += 1; $f -= 1; $f *= 1; $f /= 1; $f %= 1; if ($x === $y) { } if ($x !== $y) { } if ($x < $y) { } if ($x > $y) { } if ($x <= $y) { } if ($x >= $y) { } ++$x; } else { } while (true) { } do { } while (true); for ($i = 0; $i < 5; $i++) { } }; /** * Return record schema details given a LabelIndentifier/LabelNumber or * TransactionCode. * * #return array record schema details (or null) */ public function __construct () { print "In BaseClass constructor\n"; } }
I have made ClassWrapper exactly for these use cases. You can find inspiration here, in getPropertyNames() method: /** * Inspired by #see TokensAnalyzer. * * #return string[] */ public function getPropertyNames(): array { if ($this->propertyNames) { return $this->propertyNames; } $classOpenerPosition = $this->classToken['scope_opener'] + 1; $classCloserPosition = $this->classToken['scope_closer'] - 1; $propertyTokens = $this->findClassLevelTokensType($classOpenerPosition, $classCloserPosition, T_VARIABLE); $this->propertyNames = $this->extractPropertyNamesFromPropertyTokens($propertyTokens); $this->propertyNames = array_merge($this->propertyNames, $this->getParentClassPropertyNames()); return $this->propertyNames; }
change integer to a short string in PHP
I want to shorten long integers in PHP to a string in the same way as how hexadecimal works. I think Google also use this for there sort urls in YouTube. I have a string of numbers and letters that i want to use: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ where: 8 = 8 a = 10 f = 15 Z = 61 10 = 62 11 = 63 1a = 72 and zo on... the purpose is to set an integer the sortest way in a parameter in an affiliate url subid for tracking.
I was looking for a good name to discripe my question i find out that there is a sexagesimal numeral system that uses sixty as its base. Here you can find a function that is shorten urls and avoid fonfusion between similar characters like 0/o and i/1/l/L etc. <?php /** * A pure PHP implementation of NewBase60. A base 60 numbering system designed for * use with URL shortening. Limited/overlapping character set to avoid confusion * between similar characters, eg o/0, i/l, etc. * * Q: Why not use PHP base_convert()? * A: Because it only goes up to base 36, and doesn't support the NewBase60 * * #see http://tantek.pbworks.com/w/page/19402946/NewBase60 * #see http://en.wikipedia.org/wiki/sexagesimal * * #author david#dsingleton.co.uk */ class NewBase60 { protected static $characterSet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz'; /** * Convert a sexagesimal number to a decimal number * #param string sexagesimal number to convert * #return integer Decimal representation of sexagesimal number */ public static function toDecimal($sexNum) { // Return falsy and 0 values as is if (!$sexNum) { return $sexNum === '0' ? 0 : $sexNum; } $decNum = 0; foreach(str_split($sexNum) as $chr) { $ord = ord($chr); if ($ord>=48 && $ord<=57) { $ord -= 48; } // 0 - 9 elseif ($ord>=65 && $ord<=72) { $ord -= 55; } // A - H elseif ($ord==73 || $ord==108) { $ord = 1; } // Error correct typo: capital I, lowercase l to 1 elseif ($ord>=74 && $ord<=78) { $ord -= 56; } // J - N elseif ($ord==79) { $ord = 0; } // Error correct typo: capital O to 0 elseif ($ord>=80 && $ord<=90) { $ord -= 57; } // P - Z elseif ($ord==95) { $ord = 34; } // underscore elseif ($ord>=97 && $ord<=107) { $ord -= 62; } // a - k elseif ($ord>=109 && $ord<=122) { $ord -= 63; } // m - z else { $ord = 0; } // treat all other noise as 0 $decNum = 60 *$decNum + $ord; } return $decNum; } /** * Convert a decimal number to a sexagesimal number * #param integer Decimal number to convert * #return string sexagesimal representation of decimal */ public static function fromDecimal($decNum) { $decNum = (int) $decNum; if (!$decNum) { return $decNum === 0 ? '0' : $sexNum; } $aSexCharset = self::$characterSet; $result = ''; while ($decNum > 0) { $decRemainder = $decNum % 60; $decNum = ($decNum - $decRemainder) / 60; $result = $aSexCharset[$decRemainder] . $result; } return $result; } } Copyright: https://github.com/dsingleton/new-base-60/blob/master/newbase60.class.php
Testing if a network in cidr notation overlaps another network
I'm searching for a php algorithm that efficiently test if one cidr notated network overlaps another. Basically I have the following situation: Array of cidr adresses: $cidrNetworks = array( '192.168.10.0/24', '10.10.0.30/20', etc. ); I have a method that adds networks to the array, but this method should throw an exception when a network is added that overlaps with a network allready in the array. So ie. if 192.168.10.0/25 is added an exception should be thrown. Does anyone have/know/"can think of" an method to test this efficiently?
Here is an updated version of the class previously discussed in chat. It can do what you require, as well as many other useful things. <?php class IPv4Subnet implements ArrayAccess, Iterator { /* * Address format constants */ const ADDRESS_BINARY = 0x01; const ADDRESS_INT = 0x02; const ADDRESS_DOTDEC = 0x04; const ADDRESS_SUBNET = 0x08; /* * Constants to control whether getHosts() returns the network/broadcast addresses */ const HOSTS_WITH_NETWORK = 0x10; const HOSTS_WITH_BROADCAST = 0x20; const HOSTS_ALL = 0x30; /* * Properties to store base address and subnet mask as binary strings */ protected $address; protected $mask; /* * Counter to track the current iteration offset */ private $iteratorOffset = 0; /* * Array to hold values retrieved via ArrayAccess */ private $arrayAccessObjects = array(); /* * Helper methods */ private function longToBinary ($long) { return pack('N', $long); } private function longToDottedDecimal ($long) { return ($long >> 24 & 0xFF).'.'.($long >> 16 & 0xFF).'.'.($long >> 8 & 0xFF).'.'.($long & 0xFF); } private function longToByteArray ($long) { return array( $long >> 24 & 0xFF, $long >> 16 & 0xFF, $long >> 8 & 0xFF, $long & 0xFF ); } private function longToSubnet ($long) { if (!isset($this->arrayAccessObjects[$long])) { $this->arrayAccessObjects[$long] = new self($long); } return $this->arrayAccessObjects[$long]; } private function binaryToLong ($binary) { return current(unpack('N', $binary)); } private function binaryToDottedDecimal ($binary) { return implode('.', unpack('C*', $binary)); } private function binaryToX ($binary, $mode) { if ($mode & self::ADDRESS_BINARY) { $result = $binary; } else if ($mode & self::ADDRESS_INT) { $result = $this->binaryToLong($binary); } else if ($mode & self::ADDRESS_DOTDEC) { $result = $this->binaryToDottedDecimal($binary); } else { $result = $this->longToSubnet($this->binaryToLong($binary)); } return $result; } private function byteArrayToLong($bytes) { return ($bytes[0] << 24) | ($bytes[1] << 16) | ($bytes[2] << 8) | $bytes[3]; } private function byteArrayToBinary($bytes) { return pack('C*', $bytes[0], $bytes[1], $bytes[2], $bytes[3]); } private function normaliseComparisonSubject (&$subject) { if (!is_object($subject)) { $subject = new self($subject); } if (!($subject instanceof self)) { throw new InvalidArgumentException('Subject must be an instance of IPv4Subnet'); } } private function validateOctetArray (&$octets) { foreach ($octets as &$octet) { $octet = (int) $octet; if ($octet < 0 || $octet > 255) { return FALSE; } } return TRUE; } /* * Constructor */ public function __construct ($address = NULL, $mask = NULL) { if ($address === NULL || (is_string($address) && trim($address) === '')) { $address = array(0, 0, 0, 0); } else if (is_int($address)) { $address = $this->longToByteArray($address); } else if (is_string($address)) { $parts = preg_split('#\s*/\s*#', trim($address), -1, PREG_SPLIT_NO_EMPTY); if (count($parts) > 2) { throw new InvalidArgumentException('No usable IP address supplied: Syntax error'); } else if ($parts[0] === '') { throw new InvalidArgumentException('No usable IP address supplied: IP address empty'); } if (!empty($parts[1]) && !isset($mask)) { $mask = $parts[1]; } $address = preg_split('#\s*\.\s*#', $parts[0], -1, PREG_SPLIT_NO_EMPTY); } else if (is_array($address)) { $address = array_values($address); } else { throw new InvalidArgumentException('No usable IP address supplied: Value must be a string or an integer'); } $suppliedAddressOctets = count($address); $address += array(0, 0, 0, 0); if ($suppliedAddressOctets > 4) { throw new InvalidArgumentException('No usable IP address supplied: IP address has more than 4 octets'); } else if (!$this->validateOctetArray($address)) { throw new InvalidArgumentException('No usable IP address supplied: At least one octet value outside acceptable range 0 - 255'); } if ($mask === NULL) { $mask = array_pad(array(), $suppliedAddressOctets, 255) + array(0, 0, 0, 0); } else if (is_int($mask)) { $mask = $this->longToByteArray($mask); } else if (is_string($mask)) { $mask = preg_split('#\s*\.\s*#', trim($mask), -1, PREG_SPLIT_NO_EMPTY); switch (count($mask)) { case 1: // CIDR $cidr = (int) $mask[0]; if ($cidr === 0) { // Shifting 32 bits on a 32 bit system doesn't work, so treat this as a special case $mask = array(0, 0, 0, 0); } else if ($cidr <= 32) { // This looks odd, but it's the nicest way I have found to get the 32 least significant bits set in a // way that works on both 32 and 64 bit platforms $base = ~((~0 << 16) << 16); $mask = $this->longToByteArray($base << (32 - $cidr)); } else { throw new InvalidArgumentException('Supplied mask invalid: CIDR outside acceptable range 0 - 32'); } break; case 4: break; // Dotted decimal default: throw new InvalidArgumentException('Supplied mask invalid: Must be either a full dotted-decimal or a CIDR'); } } else if (is_array($mask)) { $mask = array_values($mask); } else { throw new InvalidArgumentException('Supplied mask invalid: Type invalid'); } if (!$this->validateOctetArray($mask)) { throw new InvalidArgumentException('Supplied mask invalid: At least one octet value outside acceptable range 0 - 255'); } // Check bits are contiguous from left // TODO: Improve this mechanism $asciiBits = sprintf('%032b', $this->byteArrayToLong($mask)); if (strpos(rtrim($asciiBits, '0'), '0') !== FALSE) { throw new InvalidArgumentException('Supplied mask invalid: Set bits are not contiguous from the most significant bit'); } $this->mask = $this->byteArrayToBinary($mask); $this->address = $this->byteArrayToBinary($address) & $this->mask; } /* * ArrayAccess interface methods (read only) */ public function offsetExists ($offset) { if ($offset === 'network' || $offset === 'broadcast') { return TRUE; } $offset = filter_var($offset, FILTER_VALIDATE_INT); if ($offset === FALSE || $offset < 0) { return FALSE; } return $offset < $this->getHostsCount(); } public function offsetGet ($offset) { if (!$this->offsetExists($offset)) { return NULL; } if ($offset === 'network') { $address = $this->getNetworkAddress(self::ADDRESS_INT); } else if ($offset === 'broadcast') { $address = $this->getBroadcastAddress(self::ADDRESS_INT); } else { // How much the address needs to be adjusted by to account for network address $adjustment = (int) ($this->getHostsCount() > 2); $address = $this->binaryToLong($this->address) + $offset + $adjustment; } return $this->longToSubnet($address); } public function offsetSet ($offset, $value) {} public function offsetUnset ($offset) {} /* * Iterator interface methods */ public function current () { return $this->offsetGet($this->iteratorOffset); } public function key () { return $this->iteratorOffset; } public function next () { $this->iteratorOffset++; } public function rewind () { $this->iteratorOffset = 0; } public function valid () { return $this->iteratorOffset < $this->getHostsCount(); } /* * Data access methods */ public function getHosts ($mode = self::ADDRESS_SUBNET) { // Parse flags and initialise vars $bin = (bool) ($mode & self::ADDRESS_BINARY); $int = (bool) ($mode & self::ADDRESS_INT); $dd = (bool) ($mode & self::ADDRESS_DOTDEC); $base = $this->binaryToLong($this->address); $mask = $this->binaryToLong($this->mask); $hasNwBc = !($mask & 0x03); $result = array(); // Get network address if requested if (($mode & self::HOSTS_WITH_NETWORK) && $hasNwBc) { $result[] = $base; } // Get hosts for ($current = $hasNwBc ? $base + 1 : $base; ($current & $mask) === $base; $current++) { $result[] = $current; } // Remove broadcast address if present and not requested if ($hasNwBc && !($mode & self::HOSTS_WITH_BROADCAST)) { array_pop($result); } // Convert to the correct type if ($bin) { $result = array_map(array($this, 'longToBinary'), $result); } else if ($dd) { $result = array_map(array($this, 'longToDottedDecimal'), $result); } else if (!$int) { $result = array_map(array($this, 'longToSubnet'), $result); } return $result; } public function getHostsCount () { $count = $this->getBroadcastAddress(self::ADDRESS_INT) - $this->getNetworkAddress(self::ADDRESS_INT); return $count > 2 ? $count - 1 : $count + 1; // Adjust return value to exclude network/broadcast addresses } public function getNetworkAddress ($mode = self::ADDRESS_SUBNET) { return $this->binaryToX($this->address, $mode); } public function getBroadcastAddress ($mode = self::ADDRESS_SUBNET) { return $this->binaryToX($this->address | ~$this->mask, $mode); } public function getMask ($mode = self::ADDRESS_DOTDEC) { return $this->binaryToX($this->mask, $mode); } /* * Stringify methods */ public function __toString () { if ($this->getHostsCount() === 1) { $result = $this->toDottedDecimal(); } else { $result = $this->toCIDR(); } return $result; } public function toDottedDecimal () { $result = $this->getNetworkAddress(self::ADDRESS_DOTDEC); if ($this->mask !== "\xFF\xFF\xFF\xFF") { $result .= '/'.$this->getMask(self::ADDRESS_DOTDEC); } return $result; } public function toCIDR () { $address = $this->getNetworkAddress(self::ADDRESS_DOTDEC); $cidr = strlen(trim(sprintf('%b', $this->getMask(self::ADDRESS_INT)), '0')); // TODO: Improve this mechanism return $address.'/'.$cidr; } /* * Comparison methods */ public function contains ($subject) { $this->normaliseComparisonSubject($subject); $subjectAddress = $subject->getNetworkAddress(self::ADDRESS_BINARY); $subjectMask = $subject->getMask(self::ADDRESS_BINARY); return $this->mask !== $subjectMask && ($this->mask | ($this->mask ^ $subjectMask)) !== $this->mask && ($subjectAddress & $this->mask) === $this->address; } public function within ($subject) { $this->normaliseComparisonSubject($subject); $subjectAddress = $subject->getNetworkAddress(self::ADDRESS_BINARY); $subjectMask = $subject->getMask(self::ADDRESS_BINARY); return $this->mask !== $subjectMask && ($this->mask | ($this->mask ^ $subjectMask)) === $this->mask && ($this->address & $subjectMask) === $subjectAddress; } public function equalTo ($subject) { $this->normaliseComparisonSubject($subject); return $this->address === $subject->getNetworkAddress(self::ADDRESS_BINARY) && $this->mask === $subject->getMask(self::ADDRESS_BINARY); } public function intersect ($subject) { $this->normaliseComparisonSubject($subject); return $this->equalTo($subject) || $this->contains($subject) || $this->within($subject); } } In order to do what you desire, the class provides 4 methods: contains() within() equalTo() intersect() Example usage of these: // Also accepts dotted decimal mask. The mask may also be passed to the second // argument. Any valid combination of dotted decimal, CIDR and integers will be // accepted $subnet = new IPv4Subnet('192.168.0.0/24'); // These methods will accept a string or another instance var_dump($subnet->contains('192.168.0.1')); //TRUE var_dump($subnet->contains('192.168.1.1')); //FALSE var_dump($subnet->contains('192.168.0.0/16')); //FALSE var_dump($subnet->within('192.168.0.0/16')); //TRUE // ...hopefully you get the picture. intersect() returns TRUE if any of the // other three match. The class also implements the Iterator interface, allowing you to iterate over all the addresses in a subnet. The iterator excludes the network and broadcast addresses, which can be retrieved separately. Example: $subnet = new IPv4Subnet('192.168.0.0/28'); echo "Network: ", $subnet->getNetworkAddress(), "; Broadcast: ", $subnet->getBroadcastAddress(), "\nHosts:\n"; foreach ($subnet as $host) { echo $host, "\n"; } The class also implements ArrayAccess, allowing you to treat it as an array: $subnet = new IPv4Subnet('192.168.0.0/28'); echo $subnet['network'], "\n"; // 192.168.0.0 echo $subnet[0], "\n"; // 192.168.0.1 // ... echo $subnet[13], "\n"; // 192.168.0.14 echo $subnet['broadcast'], "\n"; // 192.168.0.15 NB: The iterator/array methods of accessing the subnet's host addresses will return another IPv4Subnet object. The class implements __toString(), which will return the IP address as a dotted decimal if it represents a single address, or the CIDR if it represents more than one. The data can be accessed directly as a string or an integer by calling the relevant get*() method and passing the desired flag(s) (see constants defined at the top of the class). All operations are 32- and 64-bit safe. Compatibility should be (although not thoroughly tested) 5.2+ See it working For completeness, I imagine your use case would be implemented something along these lines: public function addSubnet ($newSubnet) { $newSubnet = new IPv4Subnet($newSubnet); foreach ($this->subnets as &$existingSubnet) { if ($existingSubnet->contains($newSubnet)) { throw new Exception('Subnet already added'); } else if ($existingSubnet->within($newSubnet)) { $existingSubnet = $newSubnet; return; } } $this->subnets[] = $newSubnet; } See it working
As discussed briefly in PHP chat, here's how I would implement it, to compare any two addresses. Convert the IP addresses to their binary form Extract the masks from the CIDR format Take the minimum mask of the two (least specific = contains more addresses) Use the mask on both binary representations. Compare the two. If there is a match, then one is contained within the other. Here's some example code, it's not very pretty and you'll want to adapt it to cater for your array. function bin_pad($num) { return str_pad(decbin($num), 8, '0', STR_PAD_LEFT); } $ip1 = '192.168.0.0/23'; $ip2 = '192.168.1.0/24'; $regex = '~(\d+)\.(\d+)\.(\d+)\.(\d+)/(\d+)~'; preg_match($regex, $ip1, $ip1); preg_match($regex, $ip2, $ip2); $mask = min($ip1[5], $ip2[5]); $ip1 = substr( bin_pad($ip1[1]) . bin_pad($ip1[2]) . bin_pad($ip1[3]) . bin_pad($ip1[4]), 0, $mask ); $ip2 = substr( bin_pad($ip2[1]) . bin_pad($ip2[2]) . bin_pad($ip2[3]) . bin_pad($ip2[4]), 0, $mask ); var_dump($ip1, $ip2, $ip1 === $ip2); I had trouble making it 32 bit compatible, which is why I eventually opted for converting each octet of the IP address into binary individually, and then using substr. I started off using pack('C4', $ip[1] .. $ip[4]) but when it came to using a full 32 bit mask I ran into problems converting it into binary (since PHP integers are signed). Thought for a future implementation though!
Intuitively I would suggest you'd want to do something like: Let the new entry be X Convert X to single integer form, let that integer be Y Let mask length of any entry A be mask(A) Compare any existing entries where mask(entry) = mask(Y) Mask off existing entries where mask(entry) > mask(Y) and compare with Y Mask off Y for each existing entry where mask(entry) < mask(X), such that mask(Y) = mask(entry) and compare Provided you encounter no collisions, all is well. Of course this does not check if the proposed subnet is valid. My proposition of correctness here is that I can't think of a counter-example, but there may well be one so I offer this as a basis for further thought - hope this helps.
<?php function checkOverlap ($net1, $net2) { $mask1 = explode("/", $net1)[1]; $net1 = explode("/", $net1)[0]; $netArr1 = explode(".",$net1); $mask2 = explode("/", $net2)[1]; $net2 = explode("/", $net2)[0]; $netArr2 = explode(".",$net2); $newnet1 = $newnet2 = ""; foreach($netArr1 as $num) { $binnum = decbin($num); $length = strlen($binnum); for ($i = 0; $i < 8-$length; $i++) { $binnum = '0'.$binnum; } $newnet1 .= $binnum; } foreach($netArr2 as $num) { $binnum = decbin($num); $length = strlen($binnum); for ($i = 0; $i < 8-$length; $i++) { $binnum = '0'.$binnum; } $newnet2 .= $binnum; } $length = min($mask1, $mask2); $newnet1 = substr($newnet1,0,$length); $newnet2 = substr($newnet2,0,$length); $overlap = 0; if ($newnet1 == $newnet2) $overlap = 1; return $overlap; } function networksOverlap ($networks, $newnet) { $overlap = false; foreach ($networks as $network) { $overlap = checkOverlap($network, $newnet); if ($overlap) return 1; } return $overlap; } $cidrNetworks = array( '192.168.10.0/24', '10.10.0.30/20' ); $newnet = "192.168.10.0/25"; $overlap = networksOverlap($cidrNetworks, $newnet); ?> Not sure if this is 100% correct but try it out see if it works.