Question

I have a script that I am running against a set of servers to pull hardware data off them using openwsman. This is in an attempt to keep track of changes in a very open environment as well as update a mysql db eventually to keep an inventory record. The issue is that preg_replace is adding '1''s at the beginning and end of each value in the array. These steps are rather unusual I'm sure, so I'll explain what each does and provide the output at the end.

$memsize = shell_exec("wsman enumerate http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/root/dcim/DCIM_MemoryView -h $ipaddress -V -v -c dummy.cert -P 443 -u $user -p $pass -j utf-8 -y basic | grep 'Size'");
$memsize = trim($memsize); #Removes excess spaces at beginning and end of the output.
$memsize = str_replace('          ',',',$memsize); #Replaces spaces between values with a comma.
$memsize = explode(',',$memsize); #Converts the values pulled into an array with comma as the delimiter
$memsizepreg = preg_replace("/[^0-9]/","",$memsize); #Deletes all non-number characters.
$memsizesum = array_sum($memsizepreg); #Gets a sum of all the detected DIMMs.
echo $memsize[0];
echo "<br>";
echo $memsize[1];
echo "<br>";
echo $memsize[2];
echo "<br>";
echo $memsize[3];
echo "<br>";
echo var_dump($memsize);
echo "<br>";
echo $memsizepreg[0];
echo "<br>";
echo $memsizepreg[1];
echo "<br>";
echo $memsizepreg[2];
echo "<br>";
echo $memsizepreg[3];
echo "<br>";
echo var_dump($memsizepreg);
echo "<br>";
echo $memsizesum;

In addition, if I delete the preg_replace, the array_sum gets a blank. I outputted the variables memsize, memsizepreg and memsize sum variables just to ensure that the pulled data was accurate and whatever the preg_replace is doing, is causing a 1 to be added to the beginning and end of the variable.

Output:

2048 
2048 
2048 
2048
array(4) { [0]=> string(24) "2048 " [1]=> string(24) "2048 " [2]=> string(24) "2048 " [3]=> string(23) "2048" } 
120481
120481
120481
120481
array(4) { [0]=> string(6) "120481" [1]=> string(6) "120481" [2]=> string(6) "120481" [3]=> string(6) "120481" } 
481924
Was it helpful?

Solution

As noted in the comment, someone suggested 'var_dump($array)', which revealed that the total size of the contents of the variable was 24 characters long, while the output of the variable only showed 4 characters. This was caused by XML tags still being present and getting processed by the browser. One of the tags had tags in them and when preg_replace was run, all but numbers were remaining, which removed the tags, but left the 1's from the tags. I was able to fix this by running strip_tags() after gathering the contents of the wsman command, then my values matched. Thanks again to whoever commented about var_dump. Unfortunately, you removed your comment, so I am unable to give credit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top