0

How can I convert the following object into string:

$ssh->exec('tail -1 /var/log/playlog.csv');

So I can parse the string as the first parameter in strripos():

if($idx = strripos($ssh,','))//Get the last index of ',' substring 
{
$ErrorCode = substr($ssh,$idx + 1,(strlen($ssh) - $idx) - 1); //using the found index, get the error code using substring
echo " " .$Playlist.ReturnError($ErrorCode); //The ReturnError function just replaces the error code with a custom error
}

As currently when I run my script I get the following error message:

strpos() expects parameter 1 to be string

I've seen similar questions including this one Object of class stdClass could not be converted to string , however I still can't seem to come up with a solution.

Kai
  • 93
  • 2
  • 8
  • 1
    What is the type of `$ssh`? You should `strripos()` on the value returned by `$ssh->exec()`, not on `$ssh`. – axiac Apr 03 '18 at 09:22
  • That's what I'm trying to achieve, I was thinking of taking the output from the `$ssh` variable (which is type object) into string and then parsing that string into `strripos()` – Kai Apr 03 '18 at 09:26
  • @axiac Could be, guess we'll need to see if OP replies. – chris85 Apr 03 '18 at 09:35
  • @chris85 the [`exec()`](http://php.net/manual/en/function.exec.php) function is a global function, the code posted in the question uses a method of some class (that probably wraps the functions provided by the [`SSH2`](http://php.net/manual/en/book.ssh2.php) PHP extension). They cannot be the same. – axiac Apr 03 '18 at 09:39
  • @axiac I agree the `strripos($ssh` won't work because `$ssh` is not a string so it won't work. Without the `$ssh` definition though hard to say, outside of just saying to assign the return. Assigning the return won't work if the return is an object, or even NULL. I guess i'll remove that comment until what their `exec` does is clear. – chris85 Apr 03 '18 at 09:57

1 Answers1

0

There are two problems with this line of code:

if($idx = strripos($ssh,','))
  1. $ssh is an instance of some class. You use it above as $ssh->exec(...). You should check the value it returns (probably a string) and strripos() on it, not on $ssh.

  2. strripos() returns FALSE if it cannot find the substring or a number (that can be 0) when it founds it. But in boolean context, 0 is the same as false. This means this code cannot tell apart the cases when the comma (,) is found as the first character of the string or it is not found at all.

Assuming $ssh->exec() returns the output of the remote command as string, the correct way to write this code is:

$output = $ssh->exec('tail -1 /var/log/playlog.csv');

$idx = strrpos($output, ',');        //Get the last index of ',' substring 
if ($idx !== FALSE) {
    // The value after the last comma is the error code
    $ErrorCode = substr($output, $idx + 1);
    echo ' ', $Playlist, ReturnError($ErrorCode);
} else {
   // Do something else when it doesn't contain a comma
}

There is no need to use strripos(). It performs case-insensitive comparison but you are searching for a character that is not a letter, consequently the case-sensitivity doesn't make any sense for it.

You can use strrpos() instead, it produces the same result and it's a little bit faster than strripos().


An alternative way

An alternative way to get the same outcome is to use explode() to split $output in pieces (separated by comma) and get the last piece (using end() or array_pop()) as the error code:

$output = $ssh->exec('tail -1 /var/log/playlog.csv');

$pieces = explode(',', $output);
if (count($pieces) > 1) {
    $ErrorCode = (int)end($pieces);
    echo ' ', $Playlist, ReturnError($ErrorCode);
} else {
   // Do something else when it doesn't contain a comma
}

This is not necessarily a better way to do it. It is, however, more readable and more idiomatic to PHP (the code that uses strrpos() and substr() resembles more the C code).

axiac
  • 68,258
  • 9
  • 99
  • 134
  • Assigning the output from `exec` to a variable such as `$output` is what I was after, so I can now parse this into `strripos()`, but your `strripos()` function isn't outputting anything after the last ',' . I understand this isn't related to my original question but if you're able to help I can upload the relevant code as I can't seem to figure out where I'm going wrong.. – Kai Apr 03 '18 at 11:23
  • Nonetheless, I will still accept your answer as it's the solution to the original question. – Kai Apr 03 '18 at 11:25
  • I don't get you. If `$idx` is not `FALSE` then `substr($output, $idx + 1)` returns the piece of `$output` that starts after the last comma. It returns an empty string, of course, if the comma is the last character in `$output`. – axiac Apr 03 '18 at 11:28
  • This is what my `$output` returns: `2018-03-22 12:43:21,NM_Test.h264,-2` .. there'll always be a number present after the last comma, I have a switch case `function ReturnError(res)` so I'm trying to get my `strripos()` function to look at the last output after the ',' and match it with a case inside my `ReturnError(res)` function. – Kai Apr 03 '18 at 11:44
  • The value of `$output` probably ends with a new line. You should convert the error code to integer. Check it here: https://3v4l.org/ek5tv – axiac Apr 03 '18 at 11:55
  • Will do. Thanks for your support. – Kai Apr 03 '18 at 12:35