3

I need to make the PHP below work. I basically need to take a string stored in $sshCommandResponseString and check to see if it contains 1 of 4 possibble strings.

start: Job is already running: mysql
mysql start/running, process 8019
mysql stop/waiting mysql start/running, process 8348
stop: Unknown instance: mysql start/running, process 8693

The last 3 string end with a 4 digit number which will always be a different value. So I cannot do a simple if/else check like the function below does as the string will sometimes end with a 4 digit number!

So what is the best way to check to see if my string matches 1 of these 4 strings?

function didMySqlDbRestart($sshCommandResponseString){

    if( $sshCommandResponseString == 'start: Job is already running: mysql' ){
        return true;
    }else if( $sshCommandResponseString == 'mysql start/running, process 8019' ){
        return true;
    }else if( $sshCommandResponseString == 'mysql stop/waiting mysql start/running, process 8348' ){
        return true;
    }else if( $sshCommandResponseString == 'stop: Unknown instance: mysql start/running, process 8693' ){
        return true;
    }else{
        return false;
    }
}

if(didMySqlDbRestart($sshCommandResponseString)){
    echo 'SUCCESS: MySQL Database Rebooted';
}else{
    echo 'ERROR: MySQL Database Did Not Reboot';
}

UPDATE

Adding on to user Laser's answer perhaps something like this would be ok...

  function didMySqlDbRestart($sshCommandResponseString){
    if(preg_match('/^.*\d{4}$/', trim($sshCommandResponseString)) > 0){
        return true;
    }else if($sshCommandResponseString == 'start: Job is already running: mysql'){
        return true;
    }else{
        return false;
    }
  }
1
  • Assign these 4 string to an array (so that it's dynamic nature will not affect) and do a foreach to compare Commented Jun 30, 2016 at 6:11

2 Answers 2

4

use strpos to check for the constant part of the string or use preg_match to test it with regular expression

Sign up to request clarification or add additional context in comments.

Comments

0

I am thinking regex here.

<?php
  function didMySqlDbRestart($sshCommandResponseString){
    return preg_match('/process \d+$/', trim($sshCommandResponseString)) > 0;
  }

 if(didMySqlDbRestart($sshCommandResponseString)){
    echo 'SUCCESS: MySQL Database Rebooted';
    } else{
   echo 'ERROR: MySQL Database Did Not Reboot';
 }

11 Comments

^.* in your regex is unnecessary, you also don't handle 1st case and generally just test for number at the end of the string
@Laser see my updated question at the bottom, I used your code but modified to address the issues pwolaq mentioned. If you want to do something like that in your answer.
Another thing worth mentioning. The 3 strings ending with random numbers are always prefixed with process before the numbers like this process 8019 so perhaps the regex could also look for that word before the numbers?
@JasonDavis using a regex when all you want to know is whether the string contains the substring "process" is very pointless. Just use strpos for that. It's faster and more readable.
@JasonDavis yes, so you can reliably strpos for "process" and will know that mysql has restarted, which is faster than regexing for the digits you are not interested in anyway. Likewise, you could just strpos for the fixed first string that doesn't contain the PID. Since you are apparently only interested in knowing whether MySql did or did not reboot.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.