简体   繁体   English

preg_replace在数组中的每个子字符串前后添加“ 1”

[英]preg_replace adding a '1' before and after each substring in an array

I have a script that I am running against a set of servers to pull hardware data off them using openwsman. 我有一个针对一组服务器运行的脚本,以使用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. 这样做是为了跟踪非常开放的环境中的更改以及最终更新mysql数据库以保留清单记录。 The issue is that preg_replace is adding '1''s at the beginning and end of each value in the array. 问题是preg_replace在数组中每个值的开头和结尾添加“ 1”。 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. 另外,如果删除preg_replace,则array_sum将为空。 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. 我输出变量memsize,memsizepreg和memsize sum变量只是为了确保提取的数据准确无误,并且无论preg_replace所做的是什么,都会导致将1添加到变量的开头和结尾。

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

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. 如评论中所述,有人建议使用“ var_dump($ array)”,该变量表明变量内容的总大小为24个字符长,而变量的输出仅显示4个字符。 This was caused by XML tags still being present and getting processed by the browser. 这是由于XML标签仍然存在并被浏览器处理所致。 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. 其中一个标签中包含标签,并且运行preg_replace时,除数字外的所有标签均已保留,这删除了标签,但从标签中保留了1。 I was able to fix this by running strip_tags() after gathering the contents of the wsman command, then my values matched. 在收集wsman命令的内容之后,我可以通过运行strip_tags()来解决此问题,然后我的值匹配。 Thanks again to whoever commented about var_dump. 再次感谢任何评论var_dump的人。 Unfortunately, you removed your comment, so I am unable to give credit. 不幸的是,您删除了您的评论,所以我无法赞扬您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM