Skip to content
Advertisement

Tag: preg-split

Split string by last character and save to array?

I have several strings that look like: longname1, anotherlongname2, has1numbers2init3 I would like to use str_split to split off the last character of the strings. Eg: I can get the number alone using substr($string, -1); but need to find an efficient way of retrieving the remainder of the string. I have tried: but of course this doesn’t work. Would anyone

Split string on non-alphanumeric characters and on positions between digits and non-digits

I’m trying to split a string by non-alphanumeric delimiting characters AND between alternations of digits and non-digits. The end result should be a flat array of consisting of alphabetic strings and numeric strings. I’m working in PHP, and would like to use REGEX. Examples: ES-3810/24MX should become [‘ES’, ‘3810’, ’24’, ‘MX’] CISCO1538M should become [‘CISCO’ , ‘1538’, ‘M’] The input

Explode string only once on first occurring substring

preg_match() gives one match. preg_match_all() returns all matches. preg_split() returns all splits. How can I split only on the first match? Example: This is what I want: Answer Simply set $limit to 2 for 2 parts of the array. Thanks to @BenJames for mentioning: I tested and it works fine. The limit argument: If specified, then only substrings up to

Advertisement