如何在php中从字符串中分离字母和数字 [英] How to separate letters and digits from a string in php

查看:1191
本文介绍了如何在php中从字符串中分离字母和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,由字母和数字组成.对于我的应用程序,我必须用字母和数字分隔一个字符串:例如:如果我的字符串是"12jan",则必须分别获得"12""jan".

I have a string which is combination of letters and digits. For my application i have to separate a string with letters and digits: ex:If my string is "12jan" i hav to get "12" "jan" separately..

推荐答案

您可以使用preg_split在以数字开头和以以下字母开头的点处分割字符串:

You can make use of preg_split to split your string at the point which is preceded by digit and is followed by letters as:

$arr = preg_split('/(?<=[0-9])(?=[a-z]+)/i',$str);

实施中的代码

<?php
$str = '12jan';
$arr = preg_split('/(?<=[0-9])(?=[a-z]+)/i',$str);                                                               
print_r($arr);

结果:

Array
(
    [0] => 12
    [1] => jan
)

这篇关于如何在php中从字符串中分离字母和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆