Split alphanumeric string between leading digits and trailing letters
You can use preg_split
using lookahead and lookbehind:
print_r(preg_split('#(?<=\d)(?=[a-z])#i', "0982asdlkj"));
prints
Array
(
[0] => 0982
[1] => asdlkj
)
This only works if the letter part really only contains letters and no digits.
Update:
Just to clarify what is going on here:
The regular expressions looks at every position and if a digit is before that position ((?<=\d)
) and a letter after it ((?=[a-z])
), then it matches and the string gets split at this position. The whole thing is case-insensitive (i
).
How to split number and character of string using php?
Use regex in preg_match_all()
function to select target parts of string.
$str = "B234CR45SV42";
preg_match_all("/[A-Z]+|\d+/", $str, $matches);
print_r($matches[0]);
See result in demo
Replace hyphenated range expressions with all values which exist in the range
This should get you going. This separates the sequences, adds in extra items and returns all of this as an array you can conveniently use.
$str = "Q1-Q4,A3-A6,S9-S11";
$ranges = explode (',', $str);
$output = [];
foreach ($ranges as $range) {
$items = explode ('-', $range );
$arr = [];
$fillin = [];
$letter = preg_replace('/[^A-Z]/', '', $items[0]);
$first = preg_replace('/[^0-9]/', '', $items[0]);
$last = preg_replace('/[^0-9]/', '', end($items));
for($i = $first; $i-1 < $last; $i++) {
$fillin[] = $letter . $i;
}
$output[] = $fillin;
}
var_dump( $output );
exit;
How to separate letters, numbers, undescore and dash from string PHP
You may actually use your current regex (and add .*
to it to match the string tail) with a preg_match
function and use capturing groups to capture the parts you need to get as a result. In your case, just check with preg_match
and once a match is found, assign the found captures to the $string_split
variable:
$string = "letters1234_1-someMoreText";
$string_split = $string;
if (preg_match('/^([A-Z]+[0-9]+)_([0-9])-(.*)/si', $string, $matches)) {
array_shift($matches);
$string_split = $matches;
}
print_r($string_split);
See the PHP demo yielding
Array
(
[0] => letters1234
[1] => 1
[2] => someMoreText
)
Pattern details
^
- start of string([A-Z]+[0-9]+)
- Capturing group 1: one or more letters followed with 1+ digits_
- an underscore([0-9])
- Capturing group 2: a digit-
- a hyphen(.*)
- any 0+ chars
The is
modifiers make the pattern case insensitive and .
may match line break chars.
How can I split a string into LETTERS and FLOAT/INTEGER numbers
try this:
$input = '-4D-3A';
$result = preg_split('/(-?[0-9]+\.?[0-9]*)/i', $input, 0, PREG_SPLIT_DELIM_CAPTURE);
$result=array_filter($result);
print_r($result);
It will split by numbers BUT also capture the delimiter (number)
giving : Array ( [1] => -4 [4] => D [5] => -3 [8] => A )
I've patterened number as:
1. has optional negative sign (you may want to do + too)
2. followed by one or more digits
3. followed by an optional decimal point
4. followed by zero or more digits
Can anyone point out the solution to "-0." being valid number?
How do I split the letters and numbers to 2 arrays from a string in PHP
Use preg_split
:
$string = "2w5d15h9s";
$letters = preg_split("/\d+/", $string);
array_shift($letters);
print_r($letters);
$numbers = preg_split("/[a-z]+/", $string);
array_pop($numbers);
print_r($numbers);
This prints:
Array
(
[0] => w
[1] => d
[2] => h
[3] => s
)
Array
(
[0] => 2
[1] => 5
[2] => 15
[3] => 9
)
Note that I am using array_shift
or array_pop
above to remove empty array elements which arise from the regex split. These empty entries occur because, for example, when spitting on digits the first character is a digit, which leaves behind an empty array element to the left of the first actual letter.
How to separate string to number in single word with PHP?
$input = 'AK-747';
if (preg_match('/^([a-z]{2,})-?([0-9]{2,})$/i', $input, $result)) {
unset($result[0]);
}
print_r($result);
The output:
Array
(
[1] => AK
[2] => 747
)
Related Topics
How to Run a PHP Without a Web Server
Convert Jpg/Gif Image to Png in PHP
In PHP, How to Extract Multiple E-Mail Addresses from a Block of Text and Put Them into an Array
How to Insert an Array into a Single MySQL Prepared Statement W/ PHP and Pdo
How to Pass Get Parameters to Laravel from with Get Method
When *Not* to Use Prepared Statements
How to Display Unicode Data with PHP
PHP Namespace with Dynamic Class Name
How to Enable and Use Http Put and Delete with Apache2 and PHP
Turn Off Display Errors Using File "Php.Ini"
Redirecting to Authentication Dialog - "An Error Occurred. Please Try Again Later"
PHP Split Array into Smaller Even Arrays
Max File Number Can PHP Upload at Same Time