PHP split string into integer, string and special character - php

I need to split this format of strings CF12:10 into array like below,
[0] => CF, [1] => 12, [2] => 10
Numbers and String of the provided string can be any length. I have found the php preg_match function but don't know how to make regular expression for my case. Any solution would be highly appreciated.

You could use this regex to match the individual parts:
^(\D+)(\d+):(.*)$
It matches start of string, some number of non-digit characters (\D+), followed by some number of digits (\d+), a colon and some number of characters after the : and before end-of-line. In PHP you can use preg_match to then find all the matching groups:
$input = 'CF12:10';
preg_match('/^(\D+)(\d+):(.*)$/', $input, $matches);
array_shift($matches);
print_r($matches);
Output:
Array
(
[0] => CF
[1] => 12
[2] => 10
)
Demo on 3v4l.org

Try the following code if it helps you
$str = 'C12:10';
$arr = preg_match('~^(.*?)(\d+):(.*)~m', $str, $matches);
array_shift($matches);
echo '<pre>';print_r($matches);

Related

How can I split a string into an array of substrings with regular expressions in PHP?

I am trying to break a string of binary ones and zeros into groups of four. After reading the manual and the posts I am missing something:
$subject = "101010101";
$pattern = "/.{1,4}/";
$blocks = preg_split ($pattern, $subject);
print_r($blocks);
The result is an empty array.
Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
php >
You could just use str_split() which will split your string into an array of strings of size n.
$subject = "101010101";
$split = str_split($subject, 4);
print_r($split);
Output:
Array
(
[0] => 1010
[1] => 1010
[2] => 1
)
You get that result because you are matching 1 - 4 characters to split on. It will match all the characters in the string leaving nothing to display.
If you want to use a regex to break it up into groups of 4 (and the last one because there are 9 characters) you could use preg_match_all and match only 0 or 1 using a character class instead of using a dot which will match any character except a newline.
[01]{1,4}
Regex demo | Php demo
$subject = "101010101";
$pattern = "/[01]{1,4}/";
$blocks = preg_match_all ($pattern, $subject, $matches);
print_r($matches[0]);
Result
Array
(
[0] => 1010
[1] => 1010
[2] => 1
)
Any char in a string match your pattern, in other words, any string contains only delimiters. And result contains zero-sized spaces.
The get expected result you need capture only delimiters. You can do that adding two flags
$blocks = preg_split ($pattern, $subject, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
demo
You can set PREG_SPLIT_DELIM_CAPTURE flag to get captured pattern in output
If this flag is set, parenthesized expression in the delimiter pattern
will be captured and returned as well. PHP reference
Note:- You need to add the pattern into capturing group () to get it in ouput
$subject = "101010101";
$pattern = "/(.{1,4})/";
$blocks = preg_split ($pattern, $subject, null, PREG_SPLIT_DELIM_CAPTURE);
print_r($blocks);
preg_split() returns an array containing substrings of subject split along boundaries matched by pattern, or FALSE on failure.. But you are trying to grab 1-4 characters group from that string. So preg_match_all() can be used for this purpose. Example:
$subject = "101010101";
$pattern = "/[01]{1,4}/";
preg_match_all($pattern, $subject, $match);
echo '<pre>', print_r($match[0]);

How to split string into an alphabetic string and a numeric string?

I need to use preg_split() function to split string into alpha and numeric.
Ex: ABC10000 into, ABC and 10000
GSQ39800 into GSQ and 39800
WERTYI67888 into WERTYI and 67888
Alpha characters will be the first characters(any number of) of the string always and then the numeric(any number of).
using preg_match
$keywords = "ABC10000";
preg_match("#([a-zA-Z]+)(\d+)#", $keywords, $matches);
print_r($matches);
output
Array
(
[0] => ABC10000
[1] => ABC
[2] => 10000
)
This is a tiny task. Use \K with matching on an capital letters in a character class using a one or more quantifier:
Code:
$in='WERTYI67888';
var_export(preg_split('/[A-Z]+\K/',$in));
Output:
array (
0 => 'WERTYI',
1 => '67888',
)

Regular expression to extract a numeric value on a changing position within a variable string

How can I extract the bold numeric part of a string, when the most of the string can change? /data/ is always present and followed by the relevant, variable, numeric part (in this case 123456).
differentcontentLocationhttps://example.com/api/result/13548/data/123456differentstuffincludingwhitespacesandnewlines8484
$str = "differentcontentLocationhttps://example.com/api/result/13548/data/123456differentstuffincludingwhitespacesandnewlines8484";
$str2 = "differentcontentLocationhttps://example.com/api/result/13548/data/123456";
In this example I need 123456. The only constant parts in the string are /data/ and maybe the first part of the URL, like https://.
preg_match("#/data/([0-9]+)([^0-9]+)#siU", $str, $matches);
Results in Array ( [0] => /data/123456d [1] => 123456 [2] => d ), what would be acceptable. But if there's nothing following the relevant numeric part, like in $str2, this expression fails. I've tried to make the tailing part optional with preg_match("#/ads/([0-9]+)(([^0-9]+)?)#siU", $x, $matches);, but it fails, too; returning only the first number of the numeric part.
The U greediness swapping modifier makes all greedy subpattern lazy here, you should remove it together with ([^0-9]+). You also do not need DOTALL modifier because there is no . in your pattern whose behavior could be modified with that s flag.
preg_match("#/data/([0-9]+)#i", $str, $matches);
Now, the pattern will match:
/data/ - a sequence of literal chars
([0-9]+) - Group 1 capturing 1+ digits (same as (\d+))
See the PHP demo.
$str = "differentcontentLocationhttps://e...content-available-to-author-only...e.com/api/result/13548/data/123456differentstuffincludingwhitespacesandnewlines8484";
$str2 = "differentcontentLocationhttps://e...content-available-to-author-only...e.com/api/result/13548/data/123456";
preg_match("#/data/([0-9]+)#i", $str, $matches);
print_r($matches); // Array ( [0] => /data/123456 [1] => 123456 )
preg_match("#/data/([0-9]+)#i", $str2, $matches2);
print_r($matches2); // Array ( [0] => /data/123456 [1] => 123456 )

How to split a string into an array using a given regex expression

I am trying to explode / preg_split a string so that I get an array of all the values that are enclosed in ( ). I've tried the following code but I always get an empty array, I have tried many things but I cant seem to do it right
Could anyone spot what am I missing to get my desired output?
$pattern = "/^\(.*\)$/";
$string = "(y3,x3),(r4,t4)";
$output = preg_split($pattern, $string);
print_r($output);
Current output Array ( [0] => [1] => )
Desired output Array ( [0] => "(y3,x3)," [1] => "(r4,t4)" )
With preg_split() your regex should be matching the delimiters within the string to split the string into an array. Your regex is currently matching the values, and for that, you can use preg_match_all(), like so:
$pattern = "/\(.*?\)/";
$string = "(y3,x3),(r4,t4)";
preg_match_all($pattern, $string, $output);
print_r($output[0]);
This outputs:
Array
(
[0] => (y3,x3)
[1] => (r4,t4)
)
If you want to use preg_split(), you would want to match the , between ),(, but without consuming the parenthesis, like so:
$pattern = "/(?<=\)),(?=\()/";
$string = "(y3,x3),(r4,t4)";
$output = preg_split($pattern, $string);
print_r($output);
This uses a positive lookbehind and positive lookahead to find the , between the two parenthesis groups, and split on them. It also output the same as the above.
You can use a simple regex like \B,\B to split the string and improve the performance by avoiding lookahead or lookbehind regex.
\B is a non-word boundary so it will match only the , between ) and (
Here is a working example:
http://regex101.com/r/cV7bO7/1
$pattern = "/\B,\B/";
$string = "(y3,x3),(r4,t4),(r5,t5)";
$result = preg_split($pattern, $string);
$result will contain:
Array
(
[0] => (y3,x3)
[1] => (r4,t4)
[2] => (r5,t5)
)

Match rest of string with regex

I have a string like this
ch:keyword
ch:test
ch:some_text
I need a regular expression which will match all of the strings, however, it must not match the following:
ch: (ch: is proceeded by a space, or any number of spaces)
ch: (ch: is proceeded by nothing)
I am able to deduce the length of the string with the 'ch:' in it.
Any help would be appreciated; I am using PHP's preg_match()
Edit: I have tried this:
preg_match("/^ch:[A-Za-z_0-9]/", $str, $matches)
However, this only matches 1 character after the string. I tried putting a * after the closing square bracket, but this matches spaces, which I don't want.
preg_match('/^ch:(\S+)/', $string, $matches);
print_r($matches);
\S+ is for matching 1 or more non-space characters. This should work for you.
Try this regular expression:
^ch:\S.*$
$str = <<<TEXT
ch:keyword
ch:test
ch:
ch:some_text
ch: red
TEXT;
preg_match_all('|ch\:(\S+)|', $str, $matches);
echo '<pre>'; print_r($matches); echo '</pre>';
Output:
Array
(
[0] => Array
(
[0] => ch:keyword
[1] => ch:test
[2] => ch:some_text
)
[1] => Array
(
[0] => keyword
[1] => test
[2] => some_text
)
)
Try using this:
preg_match('/(?<! +)ch:[^ ].*/', $str);

Categories