hello I have a string like this
"*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#"
I want to put all these values in an array. so I did this
$array = explode("*",$str);
Now when i printed the array the data is
Array
(
[0] =>
[1] => HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#
[2] => HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#
[3] => HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#
[4] => HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[5] => HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[6] => HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#
[7] => HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#
[8] => HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#
[9] => HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#
[10] => HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#
[11] => HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#
[12] => HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#
)
0 index is empty. I want to start the array from 0 index. Please help. what I am doing wrong here
The first element of your array is blank because your string begins with *. To solve this simply do:
$str = "*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#";
$arr = explode('*', $str);
array_shift($arr);
print_r($arr);
Which produces:
Array
(
[0] => HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#
[1] => HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#
[2] => HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#
[3] => HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[4] => HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[5] => HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#
[6] => HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#
[7] => HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#
[8] => HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#
[9] => HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#
[10] => HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#
[11] => HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#
)
You could pass it to array_filter to remove first child (index 0). It happen because you have * on first part of string. Exploding by * delimiter mean zero string on first value.
Something like:
$array = explode("*",$str);
$array = array_filter($array);
This is occurring simply because you have a "*" character at the start of the string
*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*....
If you always have that character at the start of the string, you will always have an empty index 0 in your resultant array - you need to either remove the first character before exploding the string or the first item of the array.
You might use a combination of array_filter to remove the empty values and array_values to reindex the array to start from 0:
$str = "*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#";
print_r(array_values(array_filter(explode("*", $str))));
Result:
Array
(
[0] => HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#
[1] => HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#
[2] => HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#
[3] => HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[4] => HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[5] => HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#
[6] => HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#
[7] => HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#
[8] => HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#
[9] => HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#
[10] => HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#
[11] => HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#
)
I have the following string:
objectsA=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;:objectsB=
And I want to know how can I match, optionally, the numbers inside objectsA and objectsB but put into consideration, that may one or another can be empty. For example:
objectsA can be:
objectsA=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
But also can be
objectsA=:objectsB=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
Or even
objectsA=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;:objectsB=objectsA=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
The current code:
$line2 = "
2016-07-31 00:39:00 debian-8gb-sfo2-01 gdeliveryd: notice : formatlog:trade:roleidA=3328:roleidB=2161:moneyA=0:moneyB=0:objectsA=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;:objectsB=";
if (strpos($line2, ':trade:roleidA=3328') > 0) {
if (!preg_match('/([\d-: ]+)\s*.*\sformatlog:trade:roleidA=(\d+):(.*)roleidB=(\d+):moneyA=(\d+):moneyB=(\d+):objectsA=(regexhere):objectsB=(regexhere).*$/', $line2, $c)) {
// error occured
}
echo '<pre>';
print_r($c);
}
And the problems is that the current regex ((\d+\,\d+\,\d\;)+|) has an weird behavior, that can't happen.
Output:
Array
(
[0] => 2016-07-31 00:39:00 debian-8gb-sfo2-01 gdeliveryd: notice : formatlog:trade:roleidA=3328:roleidB=2161:moneyA=0:moneyB=0:objectsA=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;:objectsB=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
[1] => 2016-07-31 00:39:00
[2] => 3328
[3] =>
[4] => 2161
[5] => 0
[6] => 0
[7] => 38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
[8] => 38155,39,1;
[9] => 38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
[10] => 38155,39,1;
)
For some reason, if the objects has the same size, the regex are creating a new array index, wich shouldn't happen.
The expected result:
Array
(
[0] => 2016-07-31 00:39:00 debian-8gb-sfo2-01 gdeliveryd: notice : formatlog:trade:roleidA=3328:roleidB=2161:moneyA=0:moneyB=0:objectsA=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;:objectsB=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
[1] => 2016-07-31 00:39:00
[2] => 3328
[4] => 2161
[5] => 0
[6] => 0
[7] => 38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
[8] => 38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
)
Regex: ^(?:\s?\d+(?:[-:]\d+){2}){2}|\w+=\K[^:]+
Details:
(?:) Non-capturing group
[] Match a single character present in the list
\K Resets the starting point of the reported match
+ Matches between one and unlimited times
| Or
PHP code:
$string = "2016-07-31 00:39:00 debian-8gb-sfo2-01 gdeliveryd: notice : formatlog:trade:roleidA=3328:roleidB=2161:moneyA=0:moneyB=0:objectsA=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;:objectsB=38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;";
preg_match_all('~^(?:\s?\d+(?:[-:]\d+){2}){2}|\w+=\K[^:]+~', $string, $matches);
print_r($matches[0]);
Output:
Array
(
[0] => 2016-07-31 00:39:00
[1] => 3328
[2] => 2161
[3] => 0
[4] => 0
[5] => 38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
[6] => 38155,54,1;38155,53,1;38155,45,1;38155,47,1;38155,46,1;2000,55,1;38155,50,1;38155,49,1;38155,48,1;38155,40,1;38155,41,1;38155,42,1;38155,43,1;38155,51,1;38155,52,1;38155,44,1;38155,35,1;38155,33,1;38155,32,1;38155,34,1;38155,36,1;38155,38,1;38155,39,1;
)
Code demo
For are navigating through this question, sometimes: less is more. The pattern (.*) will do the trick.
([\d-: ]+)\s*.*\sformatlog:trade:roleidA=(\d+):roleidB=(\d+):moneyA=(\d+):moneyB=(\d+):objectsA=(.*):objectsB=(.*).*$