i know that this is a beginner level question, but i am stuck here and i need help.
i want to store each alphabet in an array. (Only Alphabets not integers)
let me show you the previous code that i am using
$str = "ab c45 d123ef";
preg_match_all('/./us', $str, $ar);
echo '<pre>';
print_r($ar);
its output
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] =>
[3] => c
[4] => 4
[5] => 5
[6] =>
[7] => d
[8] => 1
[9] => 2
[10] => 3
[11] => e
[12] => f
)
)
But it also separate integers... what i have to change in the preg_match expression, i want this output.
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] =>
[3] => c
[4] => 45
[5] =>
[6] => d
[7] => 123
[8] => e
[9] => f
)
)
preg_match_all('/[\d.]+|./us', $str, $ar);
[\d.] matches a digit or decimal point, the + quantifier after it matches a sequence of them.
Result:
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] =>
[3] => c
[4] => 45
[5] =>
[6] => d
[7] => 123
[8] => e
[9] => f
)
)
A way with preg_split:
$result = preg_split('/(?=\pL)|(?<=\pL)/', $str, -1, PREG_SPLIT_NO_EMPTY);
You obtain:
Array
(
[0] => a
[1] => b
[2] =>
[3] => c
[4] => 45
[5] => d
[6] => 123
[7] => e
[8] => f
)
The current pattern matches two kind of positions, positions followed by a letter (?=\pL), and positions preceded by a letter (?<=\pL).
Related
I was trying to perform KMP pattern match with multiple patterns that can be stored in array but cannot got a clue in accessing values in an array.
I have a sentence say, 'hello this is me doing something'. I exploded by space and got an array of words and stored in a variable $pattern. Now I have an array with lenght 6, having six words. Now I am using str_split function on each index to get letter from it. Code is as follows
$a="hello this is me doing something";
$b=explode(" ",$a);
print_r ($b);
Got an output
Array ( [0] => hello [1] => this [2] => is [3] => me [4] => doing [5] => something )
as expected
Then I tried splting each array using str_split function
$pattern=array();
for($i=0;$i<count($b);$i++){
$pattern[$i]=str_split($b[$i]);
}
print_r($pattern);
I got following output
Array ( [0] => Array ( [0] => h [1] => e [2] => l [3] => l [4] => o ) [1] => Array ( [0] => t [1] => h [2] => i [3] => s ) [2] => Array ( [0] => i [1] => s ) [3] => Array ( [0] => m [1] => e ) [4] => Array ( [0] => d [1] => o [2] => i [3] => n [4] => g ) [5] => Array ( [0] => s [1] => o [2] => m [3] => e [4] => t [5] => h [6] => i [7] => n [8] => g ) )
Now I want every pattern to be different array which can be accessed and called separately. Like if I have [0] => Array ( [0] => h [1] => e [2] => l [3] => l [4] => o )
The expected output should be stored in different array variables dynamically.
How can I get this?
I am having an alphanumeric string
t14u1e7f8h15j4m3n50o65r22q29
I would like to transform this string into an array,in order to save space I have purposely made it that way.,Now the issue i'm facing right now is that in order to tranform it into an array using php , the order be like
data: [14, 1, 7, 8, ..]
categories : [t,u,e,f,h,j,m,n,o]
also is it possible to further transform categories into:
categories : [20:00,21:00,5:00,f,h,j,m,n,o]
where a => 1:00
b => 2:00
also since the order is like t,u,e,f,h,j is it possible to arrange them alphabetically maximum of 10 characters i.e
categories [a,b,c,d,e,f,g,i,j,k] that is 10 hours from current time..
This should work for you:
(I assume that you only use lowercase letters)
<?php
$string = "t14u1e7f8h15j4m3n50o65r22q29";
$result = array("data" => array(), "categories" => array(), "categories" => array());
preg_match_all("/\d+/", $string, $matches);
$result["data"] = $matches[0];
preg_match_all("/[^\d+]/", $string, $matches);
$result["categories"] = $matches[0];
foreach($result["categories"] as $v) {
$result["categories2"][] = ord(strtolower($v))-96;
}
print_r($result);
?>
Output:
Array
(
[data] => Array
(
[0] => 14
[1] => 1
[2] => 7
[3] => 8
[4] => 15
[5] => 4
[6] => 3
[7] => 50
[8] => 65
[9] => 22
[10] => 29
)
[categories] => Array
(
[0] => t
[1] => u
[2] => e
[3] => f
[4] => h
[5] => j
[6] => m
[7] => n
[8] => o
[9] => r
[10] => q
)
[categories2] => Array
(
[0] => 20
[1] => 21
[2] => 5
[3] => 6
[4] => 8
[5] => 10
[6] => 13
[7] => 14
[8] => 15
[9] => 18
[10] => 17
)
)
preg_split('#(?=.)(?<=.)#u','asfaaasfdf'); produces:
Array
(
[0] => a
[1] => s
[2] => f
[3] => a
[4] => a
[5] => a
[6] => s
[7] => f
[8] => d
[9] => f
)
How can I only change RegEx and get:
Array
(
[0] => as
[1] => fa
[2] => aa
[3] => sf
[4] => df
)
or:
Array
(
[0] => asf
[1] => aaa
[2] => sfd
[3] => f
)
Why use split? Use match:
preg_match_all('/.{1,3}/s', 'asfaaasfdf', $matches);
print_r($matches[0]);
Output:
Array
(
[0] => asf
[1] => aaa
[2] => sfd
[3] => f
)
How to merge two delimiters in preg_split? For example:
$str = "this is a test , and more";
$array = preg_split('/( |,)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($array);
will produce an array as
Array
(
[0] => this
[1] =>
[2] => is
[3] =>
[4] => a
[5] =>
[6] => test
[7] =>
[8] =>
[9] => ,
[10] =>
[11] =>
[12] => and
[13] =>
[14] => more
)
but I want to get
Array
(
[0] => this
[1] =>
[2] => is
[3] =>
[4] => a
[5] =>
[6] => test
[7] => ,
[8] => and
[9] =>
[10] => more
)
In fact, I want to merge the array elements when two delimiters are neighbors. In other words, ignoring the first delimiter if the next part is the second delimiter.
Try using a character class: /[ ,]+/
The + is a quantifier meaning "1 or more"
What about making sure that the situation doesn't happen in the first place :
<?php
$str = "this is a test , and more";
$str=preg_replace('/ *, */',',',$str);
$array = preg_split('/( |,)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($array);
?>
Array
(
[0] => this
[1] =>
[2] => is
[3] =>
[4] => a
[5] =>
[6] => test
[7] => ,
[8] => and
[9] =>
[10] => more
)
Using /([, ]+)/ it works. See codepad
I'm trying to explode a string character by character but I've a trouble with special characters.
I'm currently using the following function:
<?php
$input = "Comment ça va?";
$array_input = str_split($input, 1);
print_r($array_input);
?>
Here's the output:
Array (
[0] => C [1] => o [2] => m [3] => m [4] => e
[5] => n [6] => t [7] => [8] => � [9] => �
[10] => a [11] => [12] => v [13] => a [14] => ? )
I've the same issue with the line break:
Input:
"Hé!Oui?"
Output:
Array ( [0] => H [1] => � [2] => � [3] => ! [4] =>
[5] => [6] => O [7] => u [8] => i [9] => ? )
Does anybody have a solution for this issue?
Many thanks.
str_split has problems with Unicode strings.
You can use the u modifier in preg_split instead
For instance:
$input = "Comment ça va?";
$letters1 = str_split($input);
$letters2 = preg_split('//u', $input, -1, PREG_SPLIT_NO_EMPTY);
print_r($letters1);
print_r($letters2);
Will output
Array ( [0] => C [1] => o [2] => m [3] => m [4] => e
[5] => n [6] => t [7] => [8] => � [9] => �
[10] => a [11] => [12] => v [13] => a [14] => ? )
Array ( [0] => C [1] => o [2] => m [3] => m [4] => e
[5] => n [6] => t [7] => [8] => ç [9] => a
[10] => [11] => v [12] => a [13] => ? )
This is because PHP's str_split function is not multibyte-safe, i.e. it can't handle Unicode correctly. You can use this function instead, which is a multibyte-safe implementation of str_split
function mb_str_split( $string ) {
# Split at all position not after the start: ^
# and not before the end: $
return preg_split('/(?<!^)(?!$)/u', $string );
}
(source: user comments in PHP documentation)