This question already has answers here:
preg_split() String into Text and Numbers [duplicate]
(2 answers)
Closed 4 years ago.
Sorry for my bad English, I would to ask about How to split characters and numbers using preg_split(). For example, I have any data with prefix like :
ABC00001 to array(0 => 'ABC', 1 => 00001)
DEFG00002 to array(0 => 'DEFG', 1 => 00002)
AB00003 to array(0 => 'AB', 1 => 00003)
Thanks for advise
Split on the zero-length position that follows the sequence of letters.
Code: Demo
$string = 'ABC00001';
$output = preg_split('~[A-Z]+\K~', $string);
var_export($output);
The \K says forget the previous matched characters.
You can use preg_split with the PREG_SPLIT_DELIM_CAPTURE and PREG_SPLIT_NO_EMPTY flags to do what you want:
$string = 'ABC00001';
$output = preg_split('/([A-Z]+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($output);
Output:
Array ( [0] => ABC [1] => 00001 )
Demo on 3v4l.org
Related
This question already has answers here:
Split string on spaces except words in quotes
(4 answers)
Closed 3 years ago.
I'm building a website using PHP.
I am using a preg_split() to separate a given string which looks like +word1 -word2 -"word word".
But I need them in the following form +word1, -word2, -"word word".
Currently, I have this one:
$words = preg_split("/[\s\"]*\"([^\"]+)\"[\s\"]*|" . "[\s\"]*'([^']+)'[\s\"]*|" . "[\s\"]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
but it didn't work as I wish: I need to do it in this way to get it work:
+word1 -word2 '-"word word"'.
Does someone have a better regex or idea?
One option is to match from a double quote till a double quote and don't split on that match using SKIP FAIL. Then match 1+ horizontal whitespace chars to split on.
"[^"]+"(*SKIP)(*FAIL)|\h+
Regex demo | Php demo
For example
$search_expression = '+word1 -word2 -"word word"';
$words = preg_split("~\"[^\"]+\"(*SKIP)(*FAIL)|\h+~", $search_expression);
print_r($words);
Output
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)
A simpler expression with greedy ? works for matching your examples:
preg_match_all('/[+-][^+-]+ ?/', $search_expression, $matches);
print_r($matches[0]);
Yields:
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)
Se Example.
This question already has answers here:
Convert a String into an Array of Characters - multi-byte
(2 answers)
Closed 3 years ago.
Tell me, please, how to split a string character by character into an array, but so that the stresses (for example, one) in the array do not lie as a separate character, but remain with a letter?
Now it works like this, but stresses are considered a separate character.
$ arrayLetters = preg_split ('// u', $ string, NULL, PREG_SPLIT_NO_EMPTY);
Hope is's help:
$str = 'PHP';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
Output:
Array
(
[0] => P
[1] => H
[2] => P
)
This question already has answers here:
preg match count matches
(5 answers)
Closed 6 years ago.
I have a string like this:
$str = 'this is a string';
And this is my pattern: /i/g. There is three occurrence (as you see in the string above, it is containing three i). Now I need to count that. How can I get that number?
you can use substr_count() as well as preg_match_all()
echo substr_count("this is a string", "i"); // will echo 3
echo $k_count = preg_match_all('/i/i', 'this is a string', $out); // will echo 3
other method is convert into array and then count it:
$arr = str_split('this is a string');
$counts = array_count_values($arr);
print_r($counts);
output:
Array
(
[t] => 2
[h] => 1
[i] => 3
[s] => 3
[ ] => 3
[a] => 1
[r] => 1
[n] => 1
[g] => 1
)
You should use substr_count().
$str = 'this is a string';
echo substr_count($str, "i"); // 3
You can also use mb_substr_count()
$str = 'this is a string';
echo mb_substr_count($str, "i"); // 3
substr_count — Count the number of substring occurrences
mb_substr_count — Count the number of substring occurrences
preg_match_all could be a better fit.
Here is an example:
<?php
$subject = "a test string a";
$pattern = '/a/i';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
?>
Prints:
Array ( [0] => Array ( [0] => a [1] => a ) )
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a string of the format:
$15?1?2/:1$16E/:2$17?6?7/:6$19E/:7$3E/
I want to use preg_split() to break this down into an array but I can't seem to get the regex right. Specifically I want to get an array with all the numerical values directly following each $.
So in this case:
[0] => 15
[1] => 16
[2] => 17
[3] => 19
[4] => 3
If someone could explain the regex to me that would produce this that would be amazing.
Split vs. Match All
Splitting and matching are two sides of the same coin. You don't even need to split: this returns the exact array you are looking for (see PHP demo).
$regex = '~\$\K\d+~';
$count = preg_match_all($regex, $yourstring, $matches);
print_r($matches[0]);
Output
Array
(
[0] => 15
[1] => 16
[2] => 17
[3] => 19
[4] => 3
)
Explanation
\$ matches a $
The \K tells the engine to drop what was matched so far from the final match it returns
\d+ matches your digits
Hang tight for explanation. :)
Or this:
$preg = preg_match_all("/\$(\d+)/", $input, $output);
print_r($output[1]);
http://www.phpliveregex.com/p/5Rc
Here is non-regular expression example:
$string = '$15?1?2/:1$16E/:2$17?6?7/:6$19E/:7$3E/';
$array = array_map( function( $item ) {
return intval( $item );
}, array_filter( explode( '$', $string ) ) );
Idea is to explode the string by $ character, and to map that array and use the intval() to get the integer value.
Here is preg_split() example that captures the delimiter:
$string = '$15?1?2/:1$16E/:2$17?6?7/:6$19E/:7$3';
$array = preg_split( '/(?<=\$)(\d+)(?=\D|$)/', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
/*
(?<=\$) look behind to see if there is: '$'
( group and capture to \1:
\d+ digits (0-9) (1 or more times (greedy))
) end of \1
(?=\D|$) look ahead to see if there is: non-digit (all but 0-9) OR the end of the string
*/
With a help of this post, a interesting way to get every second value from resulting array.
$array = array_intersect_key( $array, array_flip( range( 1, count( $array ), 2 ) ) );
This question already has answers here:
How to limit the elements created by explode()
(4 answers)
Closed 11 months ago.
I need to split a string into exactly two parts.
Example:
$str = 'abc def ghi jkl'
Result:
$arr[0] = 'abc'
$arr[1] = 'def ghi jkl'
I tried
explode(' ', $str);
but it is giving me all the parts of the string.
How do you combine the rest of the array would be a better question i guess?
Thanks for your help~
explode(' ', $str, 2)
Read more about it at http://php.net/explode.
You need to use the limit = 2 parameter.
Example:
<?php
$str = 'one|two|three|four';
// positive limit
print_r(explode('|', $str, 2));
// negative limit (since PHP 5.1)
print_r(explode('|', $str, -1));
?>
Output
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
In your question, you can use this way:
explode(' ', $str, 2);
try the explode
explode(' ',$str , 2)
or try
$new= preg_split('/(\s)/', $str, PREG_SPLIT_DELIM_CAPTURE);