Jump to content

returning an exploded array


exius

Recommended Posts

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

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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 =/ 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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, ";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.