我有一個陣列如下
這個陣列有2行是我要比對的字串
Circuit $l_s_p vlan-id $l_v
ip address $stb_ip (applied)
然後我也提供了上面2行的3個參數值
$l_s_p = 5/1(和陣列字串一樣)
$l_v = 1250(和陣列字串不一樣)
$stb_ip = 10.153.167.155(和陣列字串不一樣)
請問要怎麼寫讓它可以知道說
其實陣列中的
1338
和
10.153.167.161
是錯的
也就是要先從陣列中
抓出關鍵的那2行出來
然後和參數再進行比對
請大家指教
謝謝大家
<?php
$a = array('Circuit 5/1 vlan-id 1338',
'Internal Circuit 5/1:1023:63/1/2/4136',
'Interface bound mod-gw',
'Current port-limit unlimited',
'ip address 10.153.167.161 (applied)',
'profile 1xSTB (applied)',
'ip source-validation 1 (applied from sub_profile)',
'ip access-group in adsl-acl (applied from sub_profile)',
'qos-policing-policy vod-mark (applied from sub_profile)',
'qos-metering-policy vod-meter (applied from sub_profile)',
'timeout idle 36000 (applied from sub_default)'
);
?>
<pre class="c" name="code">
<?php
$a = array('Circuit 5/1 vlan-id 1338',
'Internal Circuit 5/1:1023:63/1/2/4136',
'Interface bound mod-gw',
'Current port-limit unlimited',
'ip address 10.153.167.161 (applied)',
'profile 1xSTB (applied)',
'ip source-validation 1 (applied from sub_profile)',
'ip access-group in adsl-acl (applied from sub_profile)',
'qos-policing-policy vod-mark (applied from sub_profile)',
'qos-metering-policy vod-meter (applied from sub_profile)',
'timeout idle 36000 (applied from sub_default)'
);
$l_s_p = "5/1";
$l_v = "1250";
$stb_ip = "10.153.167.155";
// 方法1
$v1 = preg_grep("/^Circuit.*vlan-id/", $a);
$str1 = array_pop($v1);
if (false === strpos($str1, $l_s_p)) {
echo "$l_s_p wrong\n";
}
if (false === strpos($str1, $l_v)) {
echo "$l_v wrong\n";
}
$v2 = preg_grep("/^ip address.*(applied)/", $a);
$str2 = array_pop($v2);
if (false === strpos($str2, $stb_ip)) {
echo "$stb_ip wrong\n";
}
// 方法2
$all = implode("\n", $a);
$found = preg_match("/^Circuit\s+(.+)\s+vlan-id\s+(\d+)$/m", $all, $matches);
if ($found) {
if ($matches[1] !== $l_s_p) {
echo "$l_s_p wrong\n";
}
if ($matches[2] !== $l_v) {
echo "$l_v wrong\n";
}
}
$found = preg_match("/^ip\s+address\s+(.+)\s+\(applied\)$/m", $all, $matches);
if ($found) {
if ($matches[1] !== $stb_ip) {
echo "$stb_ip wrong\n";
}
}
weiclin 兄,謝謝你的回答
因為那個陣列算是在設備下指令其中的一個結果
所以我有可能針對不同的指令結果,設計不同的比對指令放到文字檔
例如把你的
^Circuit\s+(.+)\s+vlan-id\s+(\d+)$
^ip\s+address\s+(.+)\s+\(applied\)$
放在文字檔
剛按太快了,那如果假設這個是指令 a,所以產生陣列 a的結果
我存成 a.php 好了
那麼我在把你的
^Circuit\s+(.+)\s+vlan-id\s+(\d+)$
^ip\s+address\s+(.+)\s+\(applied\)$
存在 check_a.txt 好了
那麼要怎麼改寫你方法二的指令呢??
我不太懂你為什麼要把指令輸出的文字存為 .php 檔?
通常我是把輸出存成 .txt 檔, 再用程式去讀取 .txt 檔來分析結果
以你舉的例子, check_a.txt 裡面有兩條 regexp 規則, 總共會抓出三個數值
那是不是有可能 check_b.txt 裡面有四條規則, 共要抓出五筆數值?
於是你還得有另一個 php 程式去讀取 a.php 與 check_a.txt, 並且要知道怎樣用 check_a.txt 的規則?
還是你的 a.php 會去讀取 check_a.txt 的規則並輸出比對的結果?
php 其實挺自由的, 你講的都輕易做的到, 但邏輯上有些奇怪就是了
單純照你說的做, 就是這樣:
a.php:
<pre class="c" name="code"><?php
return array('Circuit 5/1 vlan-id 1338',
'Internal Circuit 5/1:1023:63/1/2/4136',
'Interface bound mod-gw',
'Current port-limit unlimited',
'ip address 10.153.167.161 (applied)',
'profile 1xSTB (applied)',
'ip source-validation 1 (applied from sub_profile)',
'ip access-group in adsl-acl (applied from sub_profile)',
'qos-policing-policy vod-mark (applied from sub_profile)',
'qos-metering-policy vod-meter (applied from sub_profile)',
'timeout idle 36000 (applied from sub_default)'
);
check_a.txt
<pre class="c" name="code">^Circuit\s+(.+)\s+vlan-id\s+(\d+)$
^ip\s+address\s+(.+)\s+\(applied\)$
這個用來讀取 a.php 還有 check_a.txt 並顯示結果
result_a.php
<pre class="c" name="code"><?php
// read device ooutput
$a = require 'a.php';
// read regexp rules
$rules = file_get_contents("check_a.txt");
list($rule1, $rule2) = explode("\n", $rules);
// expected values
$l_s_p = "5/1";
$l_v = "1250";
$stb_ip = "10.153.167.155";
// start check
$all = implode("\n", $a);
$found = preg_match("/$rule1/m", $all, $matches);
if ($found) {
if ($matches[1] !== $l_s_p) {
echo "$l_s_p wrong\n";
}
if ($matches[2] !== $l_v) {
echo "$l_v wrong\n";
}
}
$found = preg_match("/$rule2/m", $all, $matches);
if ($found) {
if ($matches[1] !== $stb_ip) {
echo "$stb_ip wrong\n";
}
}
$rules = file_get_contents("check_a.txt");
list($rule1, $rule2) = explode("\n", $rules);
weiclin 兄
照著你的做
結果竟然只顯示
10.153.167.155 wrong
1250 的沒顯示??
假如陣列內文字的值位置都一樣的話,運用PHP函式explode()把值取出來,在跟你的變數的值做比對即可
PHP explode():http://php.net/manual/en/function.explode.php
以下請參考一下:
<pre class="c" name="code">
<?php
header("Content-Type:text/html; charset=utf-8");
$a = array('Circuit 5/1 vlan-id 1338',
'Internal Circuit 5/1:1023:63/1/2/4136',
'Interface bound mod-gw',
'Current port-limit unlimited',
'ip address 10.153.167.161 (applied)',
'profile 1xSTB (applied)',
'ip source-validation 1 (applied from sub_profile)',
'ip access-group in adsl-acl (applied from sub_profile)',
'qos-policing-policy vod-mark (applied from sub_profile)',
'qos-metering-policy vod-meter (applied from sub_profile)',
'timeout idle 36000 (applied from sub_default)'
);
$x=explode(" ",$a[0]);
echo "\$x陣列長度:".count($x)."<br />";
for($i=0;$i<count($x);$i++){
echo "\$x[$i]=".$x[$i]."<br />";
}
?>
看完題目突然想到的,應該有更好的方法,等待其他前輩的答案