Spliting a string to fixed-size chunks using only RegEx - php

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
)

Related

Same Array Index to own Array

Hello I have the following scenario:
The following array is to be changed to a two-dimensional array.
Array
(
[0] => Array
(
[0] => Name1
[1] => Name2
[2] => Name3
[3] => Name4
[4] => Name5
[5] => Name6
[6] => Name7
[7] => Name8
)
[1] => Array
(
[0] => Company1
[1] => Company2
[2] => Company3
[3] => Company4
[4] => Company5
[5] => Company6
[6] => Company7
[7] => Company8
)
[2] => Array
(
[0] => Street1
[1] => Street2
[2] => Street3
[3] => Street4
[4] => Street5
[5] => Street6
[6] => Street7
[7] => Street8
)
[3] => Array
(
[0] => Date1
[1] => Date2
[2] => Date3
[3] => Date4
[4] => Date5
[5] => Date6
[6] => Date7
[7] => Date8
)
[4] => Array
(
[0] => Date_2_1
[1] => Date_2_2
[2] => Date_2_3
[3] => Date_2_4
[4] => Date_2_5
[5] => Date_2_6
[6] => Date_2_7
[7] => Date_2_8
)
[5] => Array
(
[0] => place1
[1] => place2
[2] => place3
[3] => place4
[4] => place5
[5] => place6
[6] => place7
[7] => place8
)
[6] => Array
(
[0] => break1
[1] => break2
[2] => break3
[3] => break4
[4] => break5
[5] => break6
[6] => break7
[7] => break8
)
[7] => Array
(
[0] => postcode1
[1] => postcode2
[2] => postcode3
[3] => postcode4
[4] => postcode5
[5] => postcode6
[6] => postcode7
[7] => postcode8
)
)
How the final array should look like
Array
(
[0] => Array
(
[0] => Name1
[1] => Company1
[2] => Street1
[3] => Date1
[4] => Date_2_1
[5] => place1
[6] => break1
[7] => postcode1
)
[1] => Array
(
[0] => Name2
[1] => Company2
[2] => Street2
[3] => Date2
[4] => Date_2_2
[5] => place2
[6] => break2
[7] => postcode2
)
[2] => Array
(
[0] => Name3
[1] => Company3
[2] => Street3
[3] => Date3
[4] => Date_2_3
[5] => place3
[6] => break3
[7] => postcode3
)
[3] => Array
(
[0] => Name4
[1] => Company4
[2] => Street4
[3] => Date4
[4] => Date_2_4
[5] => place4
[6] => break4
[7] => postcode4
)
[4] => Array
(
[0] => Name5
[1] => Company5
[2] => Street5
[3] => Date5
[4] => Date_2_5
[5] => place5
[6] => break5
[7] => postcode5
)
[5] => Array
(
[0] => Name6
[1] => Company6
[2] => Street6
[3] => Date6
[4] => Date_2_6
[5] => place6
[6] => break6
[7] => postcode6
)
[6] => Array
(
[0] => Name7
[1] => Company7
[2] => Street7
[3] => Date7
[4] => Date_2_7
[5] => place7
[6] => break7
[7] => postcode7
)
[7] => Array
(
[0] => Name8
[1] => Company8
[2] => Street8
[3] => Date8
[4] => Date_2_8
[5] => place8
[6] => break8
[7] => postcode8
)
)
function test($post_employee_nr){
require_once $_SERVER['DOCUMENT_ROOT'].'/module/dienstplan/_config.php';
$employee_query = $dbh->query("SELECT FT.*,M.*,O.*,E.*,K.* FROM
finish_time FT
LEFT JOIN
m_schicht M ON FT.m_schichtid = M.ID
LEFT JOIN
objekte O ON O.ID = M.objid
LEFT JOIN
mitarbeiter E ON E.ID = M.mitarbeiterid
LEFT JOIN
kunde K ON K.ID = M.kdid where FT.mitarbeiterid=$post_employee_nr")->fetchall();
foreach ($employee_query as $row) {
$employee_ID[] = $row['FT.ID'];
$customer[] = $row['kundenname'];
$street[] = $row['straße'];
$postcode[] = $row['plz'];
$place[] = $row['ort'];
$begin[] = $row['b_time'];
$end[] = $row['e_time'];
$break[] = $row['pause'];
$output = array($employee_ID, $customer,$street,$postcode,$place,$begin,$end,$break);
}
$html = $output;
$response = $html;
echo json_encode($response);
}
I hope I could make it obvious enough
EDIT
This is my solution:
$result = array();
foreach($employee_query as $employee_query) {
$result[] = array(
$days[date('l', strtotime($employee_query['b_time']))],
date("d.m.Y", strtotime($employee_query['b_time'])),
$employee_query['kundenname'],
$employee_query['strasse'],
$employee_query['plz'].' '.$employee_query['ort'],
date("H:i", strtotime($employee_query['b_time'])),
date("H:i", strtotime($employee_query['e_time'])),
'<i class="fas fa-plus-circle" style="color:green;"></i>'
);
}
echo json_encode($result);
exit();
}
If you want to change it to a 2 layer array just create a variable that holds the first element of the 3 layer array:
somthing like:
var array2D = array3D[0];
Also this bit of code seems like its not necesarry
$html = array($output);
$response = $html;
echo json_encode($response);
unless you need the array to be 3 layers when encoding it to Json. Otherwise just change it to:
echo json_encode($output);
Hopefully I understood your question and was able to help a little.
Edit
The way your foreach loop is currently running you're only creating a new array for each element then adding the same elements to their respective array and finally storing every array inside a new one (1 array with ALL id's, and one with ALL companynames etc...)
to fix it is very simple.
inside your foreach loop the $row variable looks like this:
$row => [idvalue, companyvalue, streetvalue etc....]
it's already an array containing the current $row's data, now all you need to do is directly add it to your $output array.
You're new foreach loop should look something like this:
foreach($employee_query as $row) {
$output[] = $row; // when using [] after a variable you add to that array
}
echo json_encode($output);
if you don't want to use all the data that your query collected you can specify which attributes you want to use like so:
$output[] = array($row['FT.ID'], $row['kundenname'], $row['straße'], etc...);

How to read values in an array with multiple array values

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?

Php Array to Table Rows

I have some form values in an array, and need to get them into table rows and could use some guidance/help. Below is what I have and am not sure if I am on the right path or not:
//These are my form values
$part = $_POST['part'];
$rel = $_POST['rel'];
$chart = $_POST['chart'];
$dob = $_POST['dob'];
$age = $_POST['age'];
$gender = $_POST['gender'];
//If participant (part) is not empty, start building the table
if ($part != "") {
//The table header (not worried about <td>/<th> semantics right now
$participants = "Participants<table border='1'>
<tr><td>Name</td><td>Relationship</td><td>Chart #</td><td>DOB</td> <td>Age</td><td>Gender</td></tr>";
//This is where I am lost...looping over and outputting on a row by row basis
foreach($part as $row) {
$participants_table = "<tr><td>". $part . "</td><td>". $rel ."</td><td>". $chart ."</td><td>". $dob ."</td><td>". $age ."</td><td>". $gender ."</td></tr>";
}
} else {
//If there are no names in the participant column(s), display the following
$participants = "No Other Participants";
$participants_table = "";
}
//Output from print_r
Next, the following may be overkill, but here goes: Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => )
Whatever you used in your print_r is what I'm focusing on here for this solution. So if you did print_r($_POST) then this would work:
I use htmlspecialchars to clean the string to prevent nasty xss injections.
<?php
foreach($_POST as $row) {
$part = htmlspecialchars($row[0], ENT_QUOTES, 'UTF-8');
$rel = htmlspecialchars($row[1], ENT_QUOTES, 'UTF-8');
$chart = htmlspecialchars($row[2], ENT_QUOTES, 'UTF-8');
$dob = htmlspecialchars($row[3], ENT_QUOTES, 'UTF-8');
$age = htmlspecialchars($row[4], ENT_QUOTES, 'UTF-8');
$gender = htmlspecialchars($row[5], ENT_QUOTES, 'UTF-8');
$unkown = htmlspecialchars($row[6], ENT_QUOTES, 'UTF-8'); //your output shows a [6]'th element
$participants_table = "<tr><td>". $part . "</td><td>". $rel ."</td><td>". $chart ."</td><td>". $dob ."</td><td>". $age ."</td><td>". $gender ."</td></tr>";
}
What's most interesting to me is that in your datadump, there's no associative keys, hence why numerical keys are used instead
when ever you are receiving an array you have to loop it using for loop or foreach loop, Since you have assigned values to variables like $part = $_POST['part']; which is not present, it will always throw error. And in your code $part is not an array to loop through.
<?php
//$part = $_POST[];
$part = array(
array('part', 'rel', 'chart', 'dob', 'age', 'gender'),
array('part1', 'rel1', 'chart1', 'dob1', 'age1', 'gender1')
);
if ($part != "") {
//The table header (not worried about <td>/<th> semantics right now
$participants = "Participants<table border='1'><tr><td>Name</td><td>Relationship</td><td>Chart #</td><td>DOB</td> <td>Age</td><td>Gender</td></tr>";
//This is where I am lost...looping over and outputting on a row by row basis
foreach ($part as $value) {
$participants .= "<tr><td>" . $value[0] . "</td><td>" . $value[1] . "</td><td>" . $value[2] . "</td><td>" . $value[3] . "</td><td>" . $value[4] . "</td><td>" . $value[5] . "</td></tr>";
}
$participants .= '</table>';
}
else {
//If there are no names in the participant column(s), display the following
$participants = "No Other Participants";
}
echo $participants;
?>

Separate each alphabet by using preg_match PHP

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).

array manipulation

I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P

Categories