开发者

Using preg_split on alpha-numeric string in PHP

开发者 https://www.devze.com 2022-12-15 05:36 出处:网络
Consider the string 12345aaa. I w开发者_JAVA技巧ant to use preg_split() on it so only the numeric part of it will be returned (i.e. 12345). How would I write the regex for it ?preg_split is not the fu

Consider the string 12345aaa. I w开发者_JAVA技巧ant to use preg_split() on it so only the numeric part of it will be returned (i.e. 12345). How would I write the regex for it ?


preg_split is not the function you want, you want preg_match as you don't want to split a string into parts, but to retrieve part of it. As I don't know more details, I can only provide a rough example of how to do it.

$reg = null;
//match the first set of 1 or more numbers
if ( preg_match('/\d+/', $str, $reg) ){
    $num = $reg[0];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消