How to find specific number in a string php? - php

I have different strings in which I want to find specific number. If the number is within the string it should print that number.
Strings are like these:
string(9) "path_12.0"
string(9) "path_12.1"
string(9) "path_13.0"
string(9) "path_13.1"
string(9) "path_13.2"
Numbers are like:
int(12)
int(12)
int(13)
int(13)
int(13)
What I tried is:
if (strpos(','.$mainString.',' , ','.$QuestionId.',') != FALSE) {
echo $QuestionId;
} // this doesn't print anything in the body
I also tried the below trick but it also doesn't print anything
if(in_array($QuestionId, explode(',', $mainString))) {
echo $QuestionId;
}
I want to check something like this:
if($questionId is in $mainString) {
echo $questionId;
}
Note: I searched similar questions on StackOverflow but I didn't found a solution which solved my issue, therefore I'm posting this question.

Another option could be to create an array with your strings and use preg_grep with a pattern that checks if the first part of the decimal is equal of one of the numbers.
Example of a pattern where the digits from the array are used as an alternation:
_\K(?:12|13)(?=\.\d+)
_\K Match underscore and forget what was matched
(?: Non capturing group
12|13 Match either 12 or 13
) Close non capturing group
(?=\.\d+) Positive lookahead, assert what is directly on the right is a dot and 1+ digits
For example:
$numbers = [12, 13];
$strings = [
"path_12.0",
"path_12.1",
"path_13.0",
"path_13.1",
"path_13.2",
"path_14.1"
];
$pattern = "/_\K(?:" . implode('|', $numbers) . ")(?=\.\d+)/";
$resullt = preg_grep($pattern, $strings);
print_r($resullt);
Result
Array
(
[0] => path_12.0
[1] => path_12.1
[2] => path_13.0
[3] => path_13.1
[4] => path_13.2
)
Php demo
Or if you want to print the numbers only, you might use array_reduce and collect the matches:
$result = array_reduce($strings, function($carry, $item) use ($pattern){
if (preg_match($pattern, $item, $matches)){
$carry[] = $matches[0];
}
return $carry;
});
print_r($result);
Result
Array
(
[0] => 12
[1] => 12
[2] => 13
[3] => 13
[4] => 13
)
Php demo

You can use below snippet,
$paths = ["path_12.0", "path_12.1", "path_13.0", "path_13.1", "path_13.2", ];
$nos = [12, 12, 13, 13, 13, ];
function strpos_arr($needle,$haystack)
{
if (!is_array($haystack)) {
$haystack = [$haystack];
}
foreach ($haystack as $what) {
if (($pos = strpos($what,(string)$needle)) !== false) {
return $pos;
}
}
return false;
}
foreach ($nos as $key => $value) {
// checking if question id in in path array with str pos
if(strpos_arr($value,$paths) !== false){
echo $value."\n";
}
}
Demo.

$array_strings = ["path_12.0", "path_12.1", "path_13.0", "path_13.1", "path_13.2"];
$array_numbers = [12, 22, 13, 11, 17];
$results = [];
foreach ($array_strings as $string){
preg_match_all('!\d+\.*\d*!', $string, $matches);
foreach ($array_numbers as $number){
if (in_array($number, $matches[0])){
array_push($results, $number);
}
}
}
print_r($results);
results: Array ( [0] => 12 [1] => 13 )
Note 1: array answers can have duplicate values.

Related

Transforming an array value into a new array without losing any characters

I have an array that looks like this:
Array
(
[basisprijs] => 17,00
[basisstaffel] =>
3-10:17;
10-20:14;
20-30:12;
30-40:10;
40-50:7,50;
50-60:6,50;
60-110:6;
[minimaalformaat] => 10x3
[maximaalformaat] => 120x5000
[breedte] => 12
[hoogte] => 4
[aantal] => 1
[Lijmlaag] => Wit(prijsberekening)+($m2*4);
)
I want to create a new array from [basisstaffel] with after each ; a new line starting, so the desired end result would be:
Array
(
[0] = > 3-10:17;
[1] = > 10-20:14;
[2] = > 20-30:12;
[3] = > 30-40:10;
[4] = > 40-50:7,50;
[5] = > 50-60:6,50;
[6] = > 60-110:6;
)
How can I do that? Using explode on the ; makes me lose that part of the value. So is there another way?
The first array is called $productarray
You could just use the explode function and after that, do a foreach loop and add the ';' symbol again, like so:
$newArray=array();
$myArray=array();
$myArray['basisprijs'] = '17,00';
$myArray['basisstaffel'] ='3-10:17;10-20:14;20-30:12;30-40:10;40-50:7,50;50-60:6,50;60-110:6';
$myArray['minimaalformaat'] = '10x3';
$myArray['maximaalformaat'] = '120x5000';
$myArray['breedte'] = '12';
$myArray['hoogte'] = '4';
$myArray['aantal'] = '1';
$myArray['Lijmlaag'] = 'Wit(prijsberekening)+($m2*4)';
$basisstafel=$myArray['basisstaffel'];
$tmp = explode(";", $basisstafel);
foreach ($tmp as $ind){
$newArray[]=$ind.';';
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
You can use preg_split and use the PREG_SPLIT_DELIM_CAPTURE flag.
This will return matches array with delimiter = 0, match = 1
https://www.php.net/manual/en/function.preg-split.php
PREG_SPLIT_DELIM_CAPTURE
If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.
<?php
$productArray = [
'basisprijs' => '17,00',
'basisstaffel' =>'
3-10:17;
10-20:14;
20-30:12;
30-40:10;
40-50:7,50;
50-60:6,50;
60-110:6;',
'minimaalformaat' => '10x3',
'maximaalformaat' => '120x5000',
'breedte' => 12,
'hoogte' => 4,
'aantal' => 1,
'Lijmlaag' => 'Wit(prijsberekening)+($m2*4);'
];
$basisstaffel = explode(";", rtrim($productArray['basisstaffel'], ';'));
var_dump($basisstaffel);
Result : array(8) { [0]=> string(14) " 3-10:17" [1]=> string(14) " 10-20:14" [2]=> string(14) " 20-30:12" [3]=> string(14) " 30-40:10" [4]=> string(16) " 40-50:7,50" [5]=> string(16) " 50-60:6,50" [6]=> string(14) " 60-110:6" }
?>
It looks like you have newlines after ; so you can preg_split by whitespace:
$result = preg_split('/\s+/', $input['basisstaffel']);
To avoid first empty item add PREG_SPLIT_NO_EMPTY flag:
$result = preg_split('/\s+/', $input['basisstaffel'], -1, PREG_SPLIT_NO_EMPTY);
Or simply use explode with \n or \r\n\ (you must know type of newlines you have):
$result = explode("\n", $input['basisstaffel']);

PHP group certain results from foreach on array into another array

I have an array that looks something like this:
$array = array( [0] => FILE-F01-E1-S01.pdf
[1] => FILE-F01-E1-S02.pdf
[2] => FILE-F01-E1-S03.pdf
[3] => FILE-F01-E1-S04.pdf
[4] => FILE-F01-E1-S05.pdf
[5] => FILE-F02-E1-S01.pdf
[6] => FILE-F02-E1-S02.pdf
[7] => FILE-F02-E1-S03.pdf );
Basically, I need to look at the first file and then get all the other files that have the same beginning ('FILE-F01-E1', for example) and put them into an array. I don't need to do anything with the other ones at this point.
I've been trying to use a foreach loop finding the previous value to do this, but am not having any luck.
Like this:
$previousFile = null;
foreach($array as $file)
{
if(substr_replace($previousFile, "", -8) == substr_replace($file, "", -8))
{
$secondArray[] = $file;
}
$previousFile = $file;
}
So then $secondArray would look like this:
Array ( [0] => FILE-F01-E1-S01.pdf [1] => FILE-F01-E1-S02.pdf
[2] => FILE-F01-E1-S03.pdf [3] => FILE-F01-E1-S04.pdf
[4] => FILE-F01-E1-S05.pdf)
As my result.
Thank you!
You can use array_filter combined with strpos:
$result = array_filter($array, function($filename) {
return strpos($filename, 'FILE-F01-E1') === 0;
});
Are you sure this will be the naming format? That is crucial information to have to construct a regexp or something to check for being a substring of the following strings.
If we can assume this and that the "base" name is always at index 0 then you could do something like.
<?php
$myArr = [
'FILE-F01-E1-S01.pdf',
'FILE-F01-E1-S02.pdf',
'FILE-F01-E1-S03.pdf',
'FILE-F01-E1-S04.pdf',
'FILE-F01-E1-S05.pdf',
'FILE-F02-E1-S01.pdf',
'FILE-F02-E1-S02.pdf',
'FILE-F02-E1-S03.pdf'
];
$baseName = '';
$allSimilarNames = [];
foreach($myArr as $index => &$name) {
if($index == 0) {
$baseName = substr($name, 0, strrpos($name, '-'));
$allSimilarNames[] = $name;
}
else {
if(strpos($name, $baseName) === 0) {
$allSimilarNames[] = $name;
}
}
}
var_dump($allSimilarNames);
This will
Check at index one to get the base name to compare against
Loop all items in the array and match all items, no matter where in the array they are, that are similar according to your naming convention
So if you next time have an array that is
$myArr = [
'FILE-F02-E1-S01.pdf',
'FILE-F01-E1-S01.pdf',
'FILE-F01-E1-S02.pdf',
'FILE-F01-E1-S03.pdf',
'FILE-F01-E1-S04.pdf',
'FILE-F01-E1-S05.pdf',
'FILE-F02-E1-S02.pdf',
'FILE-F02-E1-S03.pdf'
];
this will return all the items that match FILE-F02-E1*.
You could also make a small function of it for easier use and not have to rely on the element at index 0 having to be the "base" name.
<?php
function findMatches($baseName, &$names) {
$matches = [];
$baseName = substr($baseName, 0, strrpos($baseName, '-'));
foreach($names as &$name) {
if(strpos($name, $baseName) === 0) {
$matches[] = $name;
}
}
return $matches;
}
$myArr = [
'FILE-F01-E1-S01.pdf',
'FILE-F01-E1-S02.pdf',
'FILE-F01-E1-S03.pdf',
'FILE-F01-E1-S04.pdf',
'FILE-F01-E1-S05.pdf',
'FILE-F02-E1-S01.pdf',
'FILE-F02-E1-S02.pdf',
'FILE-F02-E1-S03.pdf'
];
$allSimilarNames = findMatches('FILE-F01-E1-S01.pdf', $myArr);
var_dump($allSimilarNames);
Run a simple foreach with strpos() which looks for an occurrence of a string within a string.
$results = array();
foreach($array as $item){
if (strpos($item, 'FILE-F01-E1') === 0) {
array_push($results, $item);
}
}
You could get the first item from the array and use explode and implode to get the part from the filename without the last hyphen and the content after that.
Then use array_filter and use substr using 0 as the start position and the length of the $fileBeginning as the length to check if the string starts with FILE-F01-E1:
$array = [
'FILE-F01-E1-S01.pdf',
'FILE-F01-E1-S02.pdf',
'FILE-F01-E1-S03.pdf',
'FILE-F01-E1-S04.pdf',
'FILE-F01-E1-S05.pdf',
'FILE-F02-E1-S01.pdf',
'FILE-F02-E1-S02.pdf',
'FILE-F02-E1-S03.pdf',
"TESTFILE-F01-E1-S03.pdf"
];
$parts = explode('-', $array[0]);
array_pop($parts);
$fileBeginning = implode('-', $parts);
$secondArray = array_filter($array, function ($x) use ($fileBeginning) {
return substr($x, 0, strlen($fileBeginning)) === $fileBeginning;
});
print_r($secondArray);
Result
Array
(
[0] => FILE-F01-E1-S01.pdf
[1] => FILE-F01-E1-S02.pdf
[2] => FILE-F01-E1-S03.pdf
[3] => FILE-F01-E1-S04.pdf
[4] => FILE-F01-E1-S05.pdf
)
Demo

PHP foreach get array key beginning with

I currently loop through the array and collect values into another array.
foreach($percentage_array[$scenario_first] as $type => $value) {
$first = substr($type,0,$first_letters_count);
if(strlen($type)==$sc_type) {
if($first==$scenario) {
$percentages[] = $value;
$scenario_array[$type] = $value;
}
}
}
Instead of looping through the array, i want to get all keys that begin with x e.g. xaa, xab, xac
So instead i do $percentage_array[$scenario_first][beginning_with_x]
How do i do this?
EDIT: This is even easier:
$filtered_array = array_filter($array, function($key){
return $key{0} == 'x';
}, ARRAY_FILTER_USE_KEY);
Giving:
array(3) {
["xa"]=>
int(1)
["xb"]=>
int(2)
["xd"]=>
int(4)
}
https://3v4l.org/Zri7n
Original answer:
Not quite sure if I understand the example code, but if you want to remove all key/value pairs in an array based on whether it begins with a letter, you can:
$array = [
'xa' => 1,
'xb' => 2,
'yc' => 3,
'xd' => 4,
];
$filtered_keys = array_filter(array_keys($array), function($k){
return !($k{0} == 'x');
});
foreach ($filtered_keys as $v) {
unset($array[$v]);
}
https://3v4l.org/6810T
Didn't try to understand your question fully, but maybe this is what you are looking for, give it a try & do modification according to your need
$percentage_array = array(
'xaa' => 1,
'xab' => 1,
'xac' => 1,
'non' => 1,
'sox' => 1);
$pattern = "/^x(.*)/";
$filtered_array = preg_filter($pattern, "$0", array_keys( $percentage_array ));
echo "<pre>";
print_r($filtered_array);
Below is the output
Array
(
[0] => xaa
[1] => xab
[2] => xac
)

Query string like parameters regex

From a text like:
category=[123,456,789], subcategories, id=579, not_in_category=[111,333]
I need a regex to get something like:
$params[category][0] = 123;
$params[category][1] = 456;
$params[category][2] = 789;
$params[subcategories] = ; // I just need to know that this exists
$params[id] = 579;
$params[not_category][0] = 111;
$params[not_category][1] = 333;
Thanks everyone for the help.
PS
As you suggested, I clarify that the structure and the number of items may change.
Basically the structure is:
key=value, key=value, key=value, ...
where value can be:
a single value (e.g. category=123 or postID=123 or mykey=myvalue, ...)
an "array" (e.g. category=[123,456,789])
a "boolean" where the TRUE value is an assumption from the fact that "key" exists in the array (e.g. subcategories)
This method should be flexible enough:
$str = 'category=[123,456,789], subcategories, id=579, not_in_category=[111,333]';
$str = preg_replace('#,([^0-9 ])#',', $1',$str); //fix for string format with no spaces (count=10,paginate,body_length=300)
preg_match_all('#(.+?)(,[^0-9]|$)#',$str,$sections); //get each section
$params = array();
foreach($sections[1] as $param)
{
list($key,$val) = explode('=',$param); //Put either side of the "=" into variables $key and $val
if(!is_null($val) && preg_match('#\[([0-9,]+)\]#',$val,$match)>0)
{
$val = explode(',',$match[1]); //turn the comma separated numbers into an array
}
$params[$key] = is_null($val) ? '' : $val;//Use blank string instead of NULL
}
echo '<pre>'.print_r($params,true).'</pre>';
var_dump(isset($params['subcategories']));
Output:
Array
(
[category] => Array
(
[0] => 123
[1] => 456
[2] => 789
)
[subcategories] =>
[id] => 579
[not_in_category] => Array
(
[0] => 111
[1] => 333
)
)
bool(true)
Alternate (no string manipulation before process):
$str = 'count=10,paginate,body_length=300,rawr=[1,2,3]';
preg_match_all('#(.+?)(,([^0-9,])|$)#',$str,$sections); //get each section
$params = array();
foreach($sections[1] as $k => $param)
{
list($key,$val) = explode('=',$param); //Put either side of the "=" into variables $key and $val
$key = isset($sections[3][$k-1]) ? trim($sections[3][$k-1]).$key : $key; //Fetch first character stolen by previous match
if(!is_null($val) && preg_match('#\[([0-9,]+)\]#',$val,$match)>0)
{
$val = explode(',',$match[1]); //turn the comma separated numbers into an array
}
$params[$key] = is_null($val) ? '' : $val;//Use blank string instead of NULL
}
echo '<pre>'.print_r($params,true).'</pre>';
Another alternate: full re-format of string before process for safety
$str = 'count=10,paginate,body_length=300,rawr=[1, 2,3] , name = mike';
$str = preg_replace(array('#\s+#','#,([^0-9 ])#'),array('',', $1'),$str); //fix for varying string formats
preg_match_all('#(.+?)(,[^0-9]|$)#',$str,$sections); //get each section
$params = array();
foreach($sections[1] as $param)
{
list($key,$val) = explode('=',$param); //Put either side of the "=" into variables $key and $val
if(!is_null($val) && preg_match('#\[([0-9,]+)\]#',$val,$match)>0)
{
$val = explode(',',$match[1]); //turn the comma separated numbers into an array
}
$params[$key] = is_null($val) ? '' : $val;//Use blank string instead of NULL
}
echo '<pre>'.print_r($params,true).'</pre>';
You can use JSON also, it's native in PHP : http://php.net/manual/fr/ref.json.php
It will be more easy ;)
<?php
$subject = "category=[123,456,789], subcategories, id=579, not_in_category=[111,333]";
$pattern = '/category=\[(.*?)\,(.*?)\,(.*?)\]\,\s(subcategories),\sid=(.*?)\,\snot_in_category=\[(.*?)\,(.*?)\]/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
I think this will get you the matches out... didn't actually test it but it might be a good starting point.
Then you just need to push the matches to the correct place in the array you need. Also test if the subcategories string exists with strcmp or something...
Also, notice that I assumed your subject string has that fixe dtype of structure... if it is changing often, you'll need much more than this...
$str = 'category=[123,456,789], subcategories, id=579, not_in_category=[111,333]';
$main_arr = preg_split('/(,\s)+/', $str);
$params = array();
foreach( $main_arr as $value) {
$pos = strpos($value, '=');
if($pos === false) {
$params[$value] = null;
} else {
$index_part = substr($value, 0, $pos);
$value_part = substr($value, $pos+1, strlen($value));
$match = preg_match('/\[(.*?)\]/', $value_part,$xarr);
if($match) {
$inner_arr = preg_split('/(,)+/', $xarr[1]);
foreach($inner_arr as $v) {
$params[$index_part][] = $v;
}
} else {
$params[$index_part] = $value_part;
}
}
}
print_r( $params );
Output :
Array
(
[category] => Array
(
[0] => 123
[1] => 456
[2] => 789
)
[subcategories] =>
[id] => 579
[not_in_category] => Array
(
[0] => 111
[1] => 333
)
)

Translate a string with point separated into array keys

I have an string (like one.two.three or month.2) and I need to translate this string exploding point character into array keys.
So I have month.2 and I need the codeline to translate this string into $lang['month'][2]
I was looking for that solution but I am not able to find it, I am blocked.
I finally found this:
<?php
$lang['one']['two']['three'] = 'well done';
$str = 'one.two.three';
$list = explode('.', $str);
$result = '$lang';
foreach ($list as $item)
{
$result .= '["'.$item.'"]';
}
var_dump(eval("return " . $result.';'));
?>
Start by exploding it:
$in = explode('.', $in);
Then rewrite it as a nested array:
$arg = array();
foreach (array_reverse($in) as $key) {
$arg = array($key => $arg);
}
And finally merge:
$out = array_merge_recursive($arg, $out);
Edit: in case you're seeking to read the array instead, then bazmegakapa's answer is what you're looking for.
I wrote a little recursive function to handle this. First you explode the string, and pass the array ($lang in your example, $a in mine) and this exploded array to GetVal(). It will do its best to return the needed value into $x.
$s="month.2";
$keys=explode('.', $s);
$a=array(
'month' => array('1' => 'fos', '2' => 'fos2'),
'retek' => 1
);
function GetVal($array, $keyarray) {
$key=array_shift($keyarray);
if (array_key_exists($key, $array)) {
if (count($keyarray)==0) {
return $array[$key];
} else {
return GetVal($array[$key], $keyarray);
}
} else {
return null;
}
}
$x=GetVal($a, $keys);
var_dump($x);
Its a clarification for the use of explode function:
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
this code explodes as:
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
not as Array[hello][world][its][a].......

Categories