PHP split string into integer element and string

后端 未结 5 1891
小蘑菇
小蘑菇 2020-11-30 11:09

I have a string say: Order_num = \"0982asdlkj\"

How can I split that into the 2 variables, with the number element and then another variable with the le

5条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 11:35

    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}).

    preg_match("/(\\d+)([a-zA-Z]+)/", "0982asdlkj", $matches);
    print("Integer component: " . $matches[1] . "\n");
    print("Letter component: " . $matches[2] . "\n");
    

    Outputs:

    Integer component: 0982
    Letter component: asdlkj
    

    http://ideone.com/SKtKs

提交回复
热议问题