Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP string comparison when string ending can contain random numbers

Tags:

php

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;
    }
  }
like image 562
JasonDavis Avatar asked Mar 05 '26 20:03

JasonDavis


1 Answers

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

like image 71
pwolaq Avatar answered Mar 07 '26 10:03

pwolaq