在前导数字和尾随字母之间拆分字母数字字符串
我有一个像这样的字符串:
$Order_num = "0982asdlkj";
如何将其拆分为 2 个变量,其中数字作为一个元素,然后另一个变量作为字母元素?
数字元素可以是 1 到 4 之间的任意长度,字母元素填充其余部分,使每个 order_num 总共有 10 个字符长。
我找到了 php explode
函数...但不知道如何在我的情况下实现它,因为数字的数量在 1 到 4 之间,并且之后的字母是随机的,所以没办法在特定字母处拆分。
I have a string like:
$Order_num = "0982asdlkj";
How can I split that into the 2 variables, with the number as one element and then another variable with the letter element?
The number element can be any length from 1 to 4 say and the letter element fills the rest to make every order_num 10 characters long in total.
I have found the php explode
function...but don't know how to make it in my case because the number of numbers is between 1 and 4 and the letters are random after that, so no way to split at a particular letter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
preg_split
使用 lookahead 和lookbehind:prints
仅当字母部分确实仅包含字母且不包含数字时才有效。
更新:
只是为了澄清这里发生的情况:
正则表达式会查看每个位置以及该位置之前是否有数字 (
(?<=\d)
) 和其后的一个字母 ((?=[az])
),然后它匹配并且字符串在该位置被分割。整个过程不区分大小写(i
)。You can use
preg_split
using lookahead and lookbehind:prints
This only works if the letter part really only contains letters and no digits.
Update:
Just to clarify what is going on here:
The regular expressions looks at every position and if a digit is before that position (
(?<=\d)
) and a letter after it ((?=[a-z])
), then it matches and the string gets split at this position. The whole thing is case-insensitive (i
).使用 preg_match() 以及正则表达式
(\d+ )([a-zA-Z]+)
。如果要将数字限制为 1-4,字母数量限制为 6-9,请将其更改为(\d+{1,4})([a-zA-Z]{6,9})< /代码>。
输出:
http://ideone.com/SKtKs
Use preg_match() with a regular expression of
(\d+)([a-zA-Z]+)
. If you want to limit the number of digits to 1-4 and letters to 6-9, change it to(\d+{1,4})([a-zA-Z]{6,9})
.Outputs:
http://ideone.com/SKtKs
您还可以使用
preg_split
来完成此操作,方法是在数字和字母之间的点拆分输入:You can also do it using
preg_split
by splitting your input at the point which between the digits and the letters:您可以为此使用正则表达式。
You can use a regex for that.
看看
关于
wazzy的内容
Check this out
with regards
wazzy
我的首选方法是 sscanf() ,因为它简洁,不需要正则表达式,提供将数字段转换为整数类型的能力,并且不会生成像
这样不必要的全字符串匹配preg_match()。不过,
%s
确实依赖于字符串的字母段中不会有空格的事实。演示
也可以设置它来声明单个变量。
如果想使用
preg_
函数,preg_split()
是最合适的,但我不会使用昂贵的环视。匹配数字,然后忘记它们(使用\K
)。这将分割字符串而不消耗任何字符。 演示要分配变量,请使用“对称数组解构”。
除了这些单一函数方法之外,还会有许多两种函数方法,例如:
但我不会在专业脚本中使用它们,因为它们缺乏优雅。
My preferred approach would be
sscanf()
because it is concise, doesn't need regex, offers the ability to cast the numeric segment as integer type, and doesn't generate needless fullstring matches likepreg_match()
.%s
does rely, though, on the fact that there will be no whitespaces in the letters segment of the string.Demo
This can also be set up to declare individual variables.
If wanting to use a
preg_
function,preg_split()
is most appropriate, but I wouldn't use expensive lookarounds. Match the digits, then forget them (with\K
). This will split the string without consuming any characters. DemoTo assign variables, use "symmetric array destructuring".
Beyond these single function approaches, there will be MANY two function approaches like:
But I wouldn't use them in a professional script because they lack elegance.