Reputation: 51
I have banged my head for the past 4 hours and I cant understand why my second trim is creating duplicates. This code with no trim works well and produces what I want.
<h1>Offline Phones</h1>
<?php
// Query For All Peers
exec("/usr/sbin/asterisk -r -x 'sip show peers'", $response);
foreach($response as $line) {
$offline = $line;
if (strpos($offline, "UNKNOWN")>0) {
print "<span style='color:red;'>$offline</span><br>";
}
}
?>
<h1>Online Phones</h1>
<?php
// Query For All Peers
exec("/usr/sbin/asterisk -r -x 'sip show peers'", $responses);
foreach($responses as $lines) {
$online = $lines;
if (strpos($online, "OK")>0) {
print "<span style='color:green;'>$online</span><br>";
}
}
?>
The above code produces: Offline Phones
302 (Unspecified) D N A 0 UNKNOWN 303 (Unspecified) D N A 0 UNKNOWN 304 (Unspecified) D N A 0 UNKNOWN 305 (Unspecified) D N A 0 UNKNOWN 306 (Unspecified) D N A 0 UNKNOWN Online Phones
301/301 192.168.1.96 D N A 45009 OK (6 ms) 307/307 192.168.1.112 D N A 50175 OK (5 ms)
BUT when I add trims (See Code below) to both querys the second query has duplicate items.
<h1>Offline Phones</h1>
<?php
// Query For All Peers
exec("/usr/sbin/asterisk -r -x 'sip show peers'", $response);
foreach($response as $line) {
$offline = $line;
if (strpos($offline, "UNKNOWN")>0)
$clean_offline = trim($offline, '(Unspecified) D N A 0 UNKNOWN');
{
echo "<span style='color:red;'>$clean_offline</span><br>";
}
}
// Query For All Peers
exec("/usr/sbin/asterisk -r -x 'sip show peers'", $responses);
foreach($responses as $lines) {
$online = $lines;
if (strpos($online, "OK")>0)
$clean_online = trim($online);
{
echo "<span style='color:green;'>$clean_online</span><br>";
}
}
?>
This code produces duplicates as shown in the output below. You will notice I don't even have a trim rule added.
302 303 304 305 306 306 306
301/301 192.168.1.96 D N A 45009 OK (5 ms) 301/301 192.168.1.96 D N A 45009 OK (5 ms) 301/301 192.168.1.96 D N A 45009 OK (5 ms) 301/301 192.168.1.96 D N A 45009 OK (5 ms) 301/301 192.168.1.96 D N A 45009 OK (5 ms) 301/301 192.168.1.96 D N A 45009 OK (5 ms) 307/307 192.168.1.112 D N A 50175 OK (5 ms) 307/307 192.168.1.112 D N A 50175 OK (5 ms)
... There should only be 301/301 192.168.1.96 D N A 45009 OK (5 ms) & 307/307 192.168.1.112 D N A 50175 OK (5 ms) here. Can anyone show me where i have went wrong?
Thanks in Advance.
Upvotes: 1
Views: 378
Reputation: 20737
This does not look correct:
foreach($response as $line) {
$offline = $line;
if (strpos($offline, "UNKNOWN")>0)
$clean_offline = trim($offline, '(Unspecified) D N A 0 UNKNOWN');
{
echo "<span style='color:red;'>$clean_offline</span><br>";
}
}
It should look like this:
foreach($response as $line)
{
// strpos returns a position if exists, ZERO is valid
// you get boolean false if it does not exist
if(strpos($line, "UNKNOWN"))
{
echo "<span style='color:red;'>".trim($line, '(Unspecified) D N A 0 UNKNOWN')"</span><br>";
}
elseif(strpos($online, "OK"))
{
echo "<span style='color:green;'>".trim($line)."</span><br>";
}
}
Upvotes: 0
Reputation: 789
You missed the {}
brackets. All the code that you want run after the if
should be contained there. so
if (strpos($offline, "UNKNOWN")>0)
$clean_offline = trim($offline, '(Unspecified) D N A 0 UNKNOWN');
{
echo "<span style='color:red;'>$clean_offline</span><br>";
}
should be
if (strpos($offline, "UNKNOWN")>0)
{
$clean_offline = trim($offline, '(Unspecified) D N A 0 UNKNOWN');
{
echo "<span style='color:red;'>$clean_offline</span><br>";
}
}
And the same with the next code block
if (strpos($online, "OK")>0)
$clean_online = trim($online);
{
echo "<span style='color:green;'>$clean_online</span><br>";
}
should be
if (strpos($online, "OK")>0)
{
$clean_online = trim($online);
{
echo "<span style='color:green;'>$clean_online</span><br>";
}
}
It's not an issue with trim()
, rather the strpos
condition which assigns $online
to $clean_online
, is only evaluated once and is used on all subsequent iterations.
Upvotes: 2