我需要使下面的PHP工作.我基本上需要获取存储在$sshCommandResponseString中的字符串,并检查它是否包含4个possibble字符串中的1个.
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
最后3个字符串以4位数字结尾,该数字始终是不同的值.因此,我无法像下面的函数那样执行简单的if / else检查,因为字符串有时会以4位数字结尾!
那么检查我的字符串是否与这4个字符串中的1个匹配的最佳方法是什么?
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';
}
更新
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;
}
}
解决方法:
我在这里正则表达式.
<?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';
}