Question

I have been bashing my head against google trying to figure this out and all I can find is people who don't understand that strpos returns the position of the found string so 0 is a valid value. That is not my problem.... in short:

$services=shell_exec('ps aux | grep udp');
print_r(is_string($services));
print_r($services);

Returns:

1
root       833  0.0  0.1   1852   768 ?        S    19:25   0:00 daemon -r -n udp-server1 /usr/bin/ncd/udp-server.php wifi:3333:true:true.
root       834  0.0  0.1   1852   768 ?        S    19:25   0:00 daemon -r -n udp-server2 /usr/bin/ncd/udp-server.php network:13000:false:false.
root       848 48.6  2.3  37036 11744 ?        R    19:25  18:07 /usr/bin/php /usr/bin/ncd/udp-server.php wifi:3333:true:true.
root       849 48.6  2.3  37064 11780 ?        R    19:25  18:07 /usr/bin/php /usr/bin/ncd/udp-server.php network:13000:false:false.
www-data  1675  0.0  0.0   1368   444 ?        S    20:02   0:00 sh -c ps aux | grep udp
www-data  1677  0.0  0.1   1456   528 ?        S    20:02   0:00 grep udp

That is the result I expect, however:

$statuses=explode("\n",file_get_contents('/var/www/misc/udp-settings.inc'));
print_r($statuses);
foreach($statuses as $status){
    print_r(strpos($services,$status)===false);
}

Returns:

Array ( [0] => wifi:3333:true:true [1] => network:13000:false:false [2] => )
1
1
1

Which is obviously wrong since the strings absolutely exist in the haystack. I have also tried stripos, mb_strpos, mb_stripos and preg_match with similar results... I have checked everything with is_string and it all checks out. I have also appended '' to the end of each string and wrapped each string with double quotes in an attempt to type cast both strings so they match (not sure if that even makes sense, but I am getting desperate LoL).

I am at a loss, I suspect there is something to do with how the udp-settings.inc file is encoded maybe, but I'm not really sure... I'm running out of things to try and could really use some help, has anyone else had this type of issue?

EDIT:

To be clear I know this works if I copy and paste the strings directly into the php file, however that isn't an option in production. One other thing I did try was exploding and imploding the value of $statuses which gives me the same result.

As mentioned in the comment below, strlen returns the appropriate results, I also checked the strings with mb_detect_encoding and they all return ASCII

Was it helpful?

Solution

This works for me just fine give it a try (the only real difference is the trim on status and the verification for empty entries on the array as well as the if/echo for the found/not found):

Live DEMO.

<?php
$services = <<<DATA
root       833  0.0  0.1   1852   768 ?        S    19:25   0:00 daemon -r -n udp-server1 /usr/bin/ncd/udp-server.php wifi:3333:true:true.
root       834  0.0  0.1   1852   768 ?        S    19:25   0:00 daemon -r -n udp-server2 /usr/bin/ncd/udp-server.php network:13000:false:false.
root       848 48.6  2.3  37036 11744 ?        R    19:25  18:07 /usr/bin/php /usr/bin/ncd/udp-server.php wifi:3333:true:true.
root       849 48.6  2.3  37064 11780 ?        R    19:25  18:07 /usr/bin/php /usr/bin/ncd/udp-server.php network:13000:false:false.
www-data  1675  0.0  0.0   1368   444 ?        S    20:02   0:00 sh -c ps aux | grep udp
www-data  1677  0.0  0.1   1456   528 ?        S    20:02   0:00 grep udp
DATA;

$statuses = <<<DATA
wifi:3333:true:true
network:13000:false:false

DATA;

$status = explode("\n",$statuses);
print_r($status);

foreach($status as $current_status)
{
    $new_status = trim($current_status);
    if (empty($new_status))
        continue;

    if (strpos($services,$new_status)!==false)
    {
        echo $new_status, " was found...\n";
    }
    else
    {
        echo $new_status, " was not found...\n";
    }
}

And with your code it should look like:

<?php
$services = shell_exec('ps aux | grep udp');
$statuses = explode("\n",file_get_contents('/var/www/misc/udp-settings.inc'));
$status = explode("\n",$statuses);
foreach($status as $current_status)
{
    $new_status = trim($current_status);
    if (empty($new_status))
        continue;

    if (strpos($services,$new_status)!==false)
    {
        echo $new_status, " was found...\n";
    }
    else
    {
        echo $new_status, " was not found...\n";
    }
}

OTHER TIPS

I can't confirm your findings, just tested with latest PHP:

<?php

$haystack =
  "root       833  0.0  0.1   1852   768 ?        S    19:25   0:00 daemon -r -n udp-server1 /usr/bin/ncd/udp-server.php wifi:3333:true:true.
  root       834  0.0  0.1   1852   768 ?        S    19:25   0:00 daemon -r -n udp-server2 /usr/bin/ncd/udp-server.php network:13000:false:false.
  root       848 48.6  2.3  37036 11744 ?        R    19:25  18:07 /usr/bin/php /usr/bin/ncd/udp-server.php wifi:3333:true:true.
  root       849 48.6  2.3  37064 11780 ?        R    19:25  18:07 /usr/bin/php /usr/bin/ncd/udp-server.php network:13000:false:false.
  www-data  1675  0.0  0.0   1368   444 ?        S    20:02   0:00 sh -c ps aux | grep udp
  www-data  1677  0.0  0.1   1456   528 ?        S    20:02   0:00 grep udp"
;

foreach ([ "wifi:3333:true:true", "network:13000:false:false" ] as $needle) {
  var_dump(strpos($haystack, $needle));
  echo PHP_EOL;
  var_dump((strpos($haystack, $needle) === false));
  echo PHP_EOL , PHP_EOL;
}

?>

Output

int(118)
bool(false)

int(257)
bool(false)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top