我需要使下面的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';
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。