str*_*ade 0 php regex preg-replace
我必须测试字符串是以00还是+开头.
伪代码:
Say I have the string **0090** or **+41**
if the string begins with **0090** return true,
elseif string begins with **+90** replace the **+** with **00**
else return false
Run Code Online (Sandbox Code Playgroud)
最后两位数字可以是0-9.
我怎么在PHP中这样做?
你可以试试:
function check(&$input) { // takes the input by reference.
if(preg_match('#^00\d{2}#',$input)) { // input begins with "00"
return true;
} elseif(preg_match('#^\+\d{2}#',$input)) { // input begins with "+"
$input = preg_replace('#^\+#','00',$input); // replace + with 00.
return true;
}else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)