我有一个php脚本来运行if else以根据条件替换值:
//$mobile = '23456701';
$mobile = '01234567';
$zero_lead = "/^01/";
$country_code = "44";
$phone = preg_replace($zero_lead, $country_code, $mobile);
if (strpos($phone, '/^0/') !== true) {
$accountNumber = $mobile;
}
else
{
$accountNumber = '';
}
$post_data = array(
'phone' => $phone,
'accountNumber' => $accountNumber,
);
echo json_encode($post_data);
返回: {"phone":"44234567","accountNumber":"01234567"}
我的预期回报,如果$mobile = '01234567';
会回报{"phone":"44234567","accountNumber":""}
并$mobile = '23456701';
会返回{"phone":"23456701","accountNumber":"23456701"}
有人有建议吗?
谢谢
PHP不是JS,因此您不能在诸如strpos之类的原始函数中使用RegExp表达式。
$mobile = '01234567';
$account = $mobile;
$country_code = '44';
if (substr($mobile, 0, 2) === '01')
{
$account = '';
$mobile = $country_code . substr($mobile, 2);
}
echo json_encode
([
'phone' => $mobile,
'accountNumber' => $account
]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句