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
Related
How can I place last index as first index in an array. Suppose I have an array which looks like this.
Array
(
[0] => ABC
[1] => abc
[2] => rodriguez
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567890
[6] =>
[7] => YES
[8] =>
[9] => 1
)
Now I want to show this array like this.
Array
(
[0] => 1
[1] => ABC
[2] => abc
[3] => rodriguez
[4] => Barkleys, 15 NO.
[5] => A
[6] => 1234567890
[7] =>
[8] => YES
[9] =>
)
How can I get this Please suggest.
Try this as an alternative way,
array_unshift($arr, $arr[count($arr)-1]);
unset($arr[count($arr)-1]);
$arr = array_values($arr);
print_r($arr);
Give it a try, this should work.
You can also do this
$ar = array('a', 'b', 'c');
//pop out the last index from the array
$shift = (array_pop($ar));
//prepend it to the beginning
array_unshift($ar, $shift);
print_r($ar);
Output:
Array (
[0] => c
[1] => a
[2] => b
)
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).
How to explode this array value:
$row = array(
[0] = 1,1,Carlyle,Rogers,1,"Carlyle, Rogers",0000-00-00,,carlyle.rogers#stafford-trust.com,,non premium)
I tried this code
$values = explode(',', $row[0]);
and give me this wrong output:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle
[6] => Rogers"
[7] => 0000-00-00
[8] =>
[9] => carlyle.rogers#stafford-trust.com
[10] =>
[11] => non premium
)
What I want is to Output must be like this one:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle, Rogers"
[6] => 0000-00-00
[7] =>
[8] => carlyle.rogers#stafford-trust.com
[9] =>
[10] => non premium
)
You can't use explode like that because your input seems to be CSV-formatted and explode knows nothing about that "format". Use str_getcsv instead:
$values = str_getcsv($row[0]);
Goal here is to take text from a log file, plain text delimited by returns with each line delimited by a varying number of spaces.
Getting it into a multi-dimensional is easy enough, getting rid of blank spaces is not. I suppose there are a lot of messy ways to go about this but isn't it one of the reasons for array_filter()?
$alarms = array(
"1530 1545 Place_4 Fault_1",
"1617 1622 Place_1 Fault_2",
"1634 1640 Place_2 Fault_1"
);
foreach ($alarms as $data) {
$subArr = explode(" ", $data);
array_filter($subArr);
print_r($subArr);
echo "<br /><br />";
}
Output:
Array ( [0] => 1530 [1] => [2] => [3] => [4] => [5] => 1545 [6] => [7] => [8] => [9] => [10] => Place_4 [11] => [12] => [13] => [14] => [15] => Fault_1 )
Array ( [0] => 1617 [1] => [2] => [3] => [4] => [5] => 1622 [6] => [7] => [8] => [9] => [10] => Place_1 [11] => [12] => [13] => [14] => [15] => Fault_2 )
Array ( [0] => 1634 [1] => [2] => [3] => [4] => [5] => 1640 [6] => [7] => [8] => [9] => [10] => Place_2 [11] => [12] => [13] => [14] => [15] => Fault_1 )
Want it to be:
Array ( [0] => 1530 [1] => 1545 [2] => Place_4 [3] => Fault_1 )
Array ( [0] => 1617 [1] => 1622 [2] => Place_1 [3] => Fault_2 )
Array ( [0] => 1634 [1] => 1640 [2] => Place_2 [3] => Fault_1 )
Not sure what's wrong...
Cheers.
From the manual, array_filter:
Return values:
Returns the filtered array.
Emphasis on returns. Currently you ignore the return value.
Rather than explode(" ",$something), try this:
$parts = preg_split("/ +/",$something);
This will split on variable numbers of spaces.
updated code
$alarms = array(
"1530 1545 Place_4 Fault_1",
"1617 1622 Place_1 Fault_2",
"1634 1640 Place_2 Fault_1"
);
foreach ($alarms as $data) {
$subArr = explode(" ", $data);
$subArr = array_filter($subArr);
print_r($subArr);
echo "<br /><br />";
}
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)