我需要从下面的单个字符串中分割数据,
$string = "RC9999999999A202";
//Need to split as follows:
$code = "RC"; //This value must be of 2 alphabetic characters
$phone = "9999999999"; //This value must be of 10 digits
$amount = "202"; //This value can be of any length (numeric only)
我没有尝试任何东西,因为我不知道这个,因为我是新手。
请帮忙!
请不要留下负面评价,而是试着帮助我。
答案 0 :(得分:1)
如果你的字符串总是像你描述 2 字符那么 10 数字后跟任意数量的字符,那么你只需使用{{1}即可得到你想要的字符串像这样:
substr
注意:此方法不验证任何数据,只是提取根据您在问题中描述的内容在字符串中提供的内容。
答案 1 :(得分:0)
您可以将此正则表达式用于preg_match
:
$str = 'RC9999999999A202';
if (preg_match('/([A-Z]{2})(\d{10})\D*(\d*)/', $str, $m)) {
unset($m[0]); // delete full match from array
print_r($m);
}
<强>输出:强>
Array
(
[1] => RC
[2] => 9999999999
[3] => 202
)
答案 2 :(得分:0)
这将根据您的规格进行匹配,并填充变量代码,电话和金额。
$string = "RC9999999999A202";
Preg_match("/([A-Z]{2})(\d{10})A(\d+)/", $string, $match);
List($string, $code, $phone, $amount) = $match;
此模式假定“A”始终为“A” 你在问题中根本没有提到它,所以我认为它总是一个“A”。