exius Posted March 22, 2010 Share Posted March 22, 2010 Hey Coders, I've been staring at this function for way too long and need your support. Simply, I am trying to get the exploded array of values to return back. Nothing has worked well for me... function set_ports($ports) { $ports_array = explode(",",$ports); foreach($ports_array as $key=>$val) { if(ereg("([0-9]+)\-([0-9]+)",$val, $buff)) { for($ii=$buff[1]; $ii<=$buff[2]; $ii++) { $this->ports[] = $ii; } } else { $this->ports[] = $val; } } } So, after running the following: $my_scanner->set_ports(1-5,80); ... I should have a return of 1,2,3,4,5,80 if you can get it to work. Please check it out! Thanks. exius Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/ Share on other sites More sharing options...
ignace Posted March 22, 2010 Share Posted March 22, 2010 class MyScanner { public function addPorts() { if (func_num_args() === 2) { $range = range(func_get_arg(0), func_get_arg(1)); $this->_addPorts($range); } else { $this->_addPorts(func_get_arg(0)); } return $this; } private function _addPorts($array) { $array = (array) $array; $this->ports = array_merge($this->ports, $array); } } //Use as: print_r($scanner->addPorts(1, 5)->addPorts(80)->getPorts()); Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/#findComment-1030243 Share on other sites More sharing options...
exius Posted March 23, 2010 Author Share Posted March 23, 2010 Thanks for your quick reply ignace! So... I tried running it and get this error: Fatal error: Call to a member function addPorts() on a non-object in for: class MyScanner { public function addPorts() { if (func_num_args() === 2) { $range = range(func_get_arg(0), func_get_arg(1)); $this->_addPorts($range); } else { $this->_addPorts(func_get_arg(0)); } return $this; } private function _addPorts($array) { $array = (array) $array; $this->ports = array_merge($this->ports, $array); } } print_r($MyScanner->addPorts(1,5)->addPorts(80)->getPorts()); I know... there must be something real obvious that I have not modified. Unfortunately, I'm still novice =/ Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/#findComment-1030284 Share on other sites More sharing options...
oni-kun Posted March 23, 2010 Share Posted March 23, 2010 I know... there must be something real obvious that I have not modified. Unfortunately, I'm still novice =/ I think he must've wrote it in a hurry, He didn't initialize the object. Instead of the Before the print_r part add: $MyScanner = new MyScanner; //Initialize object to be used Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/#findComment-1030290 Share on other sites More sharing options...
exius Posted March 23, 2010 Author Share Posted March 23, 2010 I know... there must be something real obvious that I have not modified. Unfortunately, I'm still novice =/ I think he must've wrote it in a hurry, He didn't initialize the object. Instead of the Before the print_r part add: $MyScanner = new MyScanner; //Initialize object to be used Added that, corrected some errors, but now i'm stuck... class MyScanner { public function addPorts() { if (func_num_args() === 2) { $f1 = func_get_arg(1); $f2 = func_get_arg(0); $range = range($f1,$f2); $this->_addPorts($range); } else { $f3 = func_get_arg(0); $this->_addPorts($f3); } return $this; } private function _addPorts($array) { $array = (array) $array; $this->ports = array_merge($this->ports, $array); //Warning: array_merge() [function.array-merge]: Argument #1 is not an array (got twice) } } $MyScanner = new MyScanner; //Initialize object to be used $scan1 = $MyScanner->addPorts(1,5); $scan2 = $MyScanner->addPorts(80); print_r($MyScanner->$scan1->$scan2);//->getPorts()); //Catchable fatal error: Object of class MyScanner could not be converted to string As you can see, I commented the output errors on the lines responsible. Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/#findComment-1030327 Share on other sites More sharing options...
trq Posted March 23, 2010 Share Posted March 23, 2010 Its not a complete example, you'll need to write the rest yourself. It needs a private ports[/i[ property and a getPorts() method at minimum. Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/#findComment-1030353 Share on other sites More sharing options...
exius Posted March 26, 2010 Author Share Posted March 26, 2010 Man, this is killing me. Jumping the gun ain't easy for me in OO coding. How can I use getPorts() to return the array created by addPorts() & _addPorts()? What it looks like now... class MyScanner { public function addPorts() { if (func_num_args() === 2) { $f1 = func_get_arg(1); $f2 = func_get_arg(0); $range = range($f1,$f2); $this->_addPorts($range); } else { $f3 = func_get_arg(0); $this->_addPorts($f3); } return $this; } private $ports = ""; private function _addPorts($array) { $array = (array) $array; $this->ports = array_merge($this->ports, $array); //Warning: array_merge() [function.array-merge]: Argument #1 is not an array } public function getPorts() { return $this; } } $MyScanner = new MyScanner; $scan1 = $MyScanner->addPorts(1,5); $scan2 = $MyScanner->addPorts(80); print_r($MyScanner->getPorts()); Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/#findComment-1032413 Share on other sites More sharing options...
exius Posted March 26, 2010 Author Share Posted March 26, 2010 to be specific: I am looking to expand a mixed data set which includes range and singles like $portArray= array(getPorts(1-5,80,443)) whereas $portArray should look like array(1,2,3,4,5,80,443). I hope I am making sense. Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/#findComment-1032446 Share on other sites More sharing options...
trq Posted March 27, 2010 Share Posted March 27, 2010 public function getPorts() { return $this->ports; } Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/#findComment-1032565 Share on other sites More sharing options...
exius Posted April 2, 2010 Author Share Posted April 2, 2010 I didn't like the fact that I had to split up my port ranges and individual ports. That method was not as linear as I intended. So I wrote this out a different way where I can scan multiple ranges and single ports... the way easiest for me at least: function expanding_port_array($pp){ $pp = explode(",",$pp); foreach($pp as $key=>$val) { if(ereg("([0-9]+)\-([0-9]+)",$val, $buff)) { for($ii=$buff[1]; $ii<=$buff[2]; $ii++) { if(is_array($hi)){ array_push($hi,$ii); } else{ $hi = array($ii); } } } else { if(is_array($hi)){ array_push($hi,$val); } else{ $hi = array($val); } } } return $hi; } $scan_ports = "21-23,80,123-125,1337"; $expanded_ports = expanding_port_array($scan_ports); foreach($expanded_ports as $one_port){ echo "$one_port, "; } Quote Link to comment https://forums.phpfreaks.com/topic/196181-returning-an-exploded-array/#findComment-1036021 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.