Regex to match multiple one and two digit numbers [duplicate] - php

This question already has answers here:
Regex matching 5-digit substrings not enclosed with digits
(2 answers)
Retrieve exactly 1 digit using regular expression in python
(3 answers)
Closed 1 year ago.
I'm looking to match numbers that are either 1 or 2 digit (but not more) long so I can add leading zeroes. My input strings look like these:
LL.1 ETX 189
1.20.3.4[8]
1.30.4[17]
229.c.85.8[4]
62.c.80.11
RR 60 H 2
R.60 H 2
Y.11.25[28]
Abc T.14.55
LL 108 ETX 189
Content 1364
With the application of the regex I would hope to identify the following:
LL.1 ETX 189 => match: 1
1.20.300.4[8] => match: 1, 20, 4, 8
1.30.4[17] => match: 1, 30, 4, 17
229.c.85.8[4] => match: 85,8,4
62.c.80.11 => match: 62, 80, 11
RR 60 H 2 => match: 60, 2
R.60 H 2 => match: 60, 2
Y.11.25[28] => match: 11, 25, 28
Abc T.14.55 => match: 14, 55
Whilst ignoring:
LL 108 ETX 189
Content 1364
I thought doing something as simple as: ([1-90-9]{1,2}) would work but this returns the following:
LL.1 ETX 189 => match: 1, 18, 9
1.20.300.4[8] => match: 1, 20, 30, 0, 4, 8
1.30.4[17] => match: 1, 30, 4, 17
229.c.85.8[4] => match: 22, 9, 85,8,4
62.c.80.11 => match: 62, 80, 11
RR 60 H 2 => match: 60, 2
R.60 H 2 => match: 60, 2
Y.11.25[28] => match: 11, 25, 28
Abc T.14.55 => match: 14, 55
LL 108 ETX 189 => match: 10, 8, 18, 9
Content 1364 => match: 13, 64
Any suggestions on how I can achieve this?
I've had a look at this question: 'RegEx Expression /w limited length and chars' and this one: Regex match one digit or two..

Related

How do i find the occurence of words of particular number

Hello guys I have a small question that suppose I have a string as
"Hello My name is XYZ"
Now I know I can find the length of the words as "Hello" has 5 characters and "My" has 2 characters. By using following code
$text = file_get_contents('text.txt'); // $text = 'Hello my name is XYZ';
$words = str_word_count($text, 1);
$wordsLength = array_map(
function($word) { return mb_strlen($word, 'UTF-8'); },
$words
);
var_dump(array_combine($words, $wordsLength));
But what if i want to find that the number of words with length 1 is 0. The number of words with lengths 2 is 2. The number of words with length 3 is 1 and so on till number of length 10
Note- I am considering the word length till there is a space Suppose there is a date in the data like 20.04.2016 so it should show me that the number is words with length 10 is 1.
and one more thing how do I find the average length for the words in the string.
Thank you in advance
If you use array_count_values() on the $wordsLength array it will give a count of the string lengths there are. If you use this and a template array (created using array_fill()) with the elements 1-10 and a value of 0. You will get a list of all of the word counts...
$counts = array_replace(array_fill(1, 9, 0),
array_count_values($wordsLength));
will give...
Array
(
[1] => 0
[2] => 2
[3] => 1
[4] => 1
[5] => 1
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
Hi try this it works on the date and special chars,emojis
$text = 'Hello 20.04.2016 🚩 my face😘face is XYZ';
$words =preg_split('/\s+/', $text);
$wordsLength = array_map(
function($word) { return mb_strlen($word, 'UTF-8'); } ,$words);
print_r($words);
//Get Average word Length
$avg=round(array_sum($wordsLength)/count($words),1);
//print Avg
print($avg);
?>
(Demo)
$text = ' Hello 20.04.2016 🚩 my incredibleness face😘face is XYZ ';
Generate array of continuous visible characters
$words = preg_split('/\s+/', $text, 0, PREG_SPLIT_NO_EMPTY);
array (
0 => 'Hello',
1 => '20.04.2016',
2 => '🚩',
3 => 'my',
4 => 'incredibleness',
5 => 'face😘face',
6 => 'is',
7 => 'XYZ',
)
Replace visible strings with their multibyte length notice the simpler syntax
$wordsLength = array_map('mb_strlen', $words);
array (
0 => 5,
1 => 10,
2 => 1,
3 => 2,
4 => 14,
5 => 9,
6 => 2,
7 => 3,
)
Group and count lengths
$lengthCounts = array_count_values($wordsLength);
array (
5 => 1,
10 => 1,
1 => 1,
2 => 2,
14 => 1,
9 => 1,
3 => 1,
)
Establish an array of defaults, because $lengthCounts may have gaps
$defaultCounts = array_fill_keys(range(1,10), 0);
array (
1 => 0,
2 => 0,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
7 => 0,
8 => 0,
9 => 0,
10 => 0,
)
Filter the counts to remove elements/counts that are out-of-range
$filteredCounts = array_intersect_key($lengthCounts, $defaultCounts);
array (
5 => 1,
10 => 1,
1 => 1,
2 => 2,
9 => 1,
3 => 1,
)
Overwrite the defaults with found counts to prevent gaps in the output
$gaplessCounts = array_replace($defaultCounts, $filteredCounts);
array (
1 => 1,
2 => 2,
3 => 1,
4 => 0,
5 => 1,
6 => 0,
7 => 0,
8 => 0,
9 => 1,
10 => 1,
)

How to decode from an unknown encoding in PHP?

My question is different from this one. I am trying to fix a broken encoding but I don't know how to proceed.
In my database I have this name:
mysql> select filename from file WHERE filename LIKE 'MAC%';
+-------------------------------------------------+
| filename |
+-------------------------------------------------+
| MAC-1600PVå–扱説明書.pdf |
+-------------------------------------------------+
1 row in set (0.00 sec)
But on my filesystem the file is named:
$ ls files/*MAC*
files/MAC-1600PV取扱説明書.pdf
I have tried to unpack both strings from PHP and the content differ:
The utf-8 sequence read from the filesystem:
=> "MAC-1600PV取扱説明書"
>>> unpack('C*', $u)
...
7 => 48,
8 => 48,
9 => 80,
10 => 86,
11 => 195,
12 => 165,
13 => 226,
14 => 128,
15 => 147,
16 => 195,
17 => 166,
18 => 226,
And for the one read from the database:
...
7 => 48,
8 => 48,
9 => 80,
10 => 86,
11 => 229,
12 => 143,
13 => 150,
14 => 230,
15 => 137,
16 => 177,
So at some-point I lost the original encoding and I have no clue of how to fix my database which is in utf8mb4.
Any advice?

How to Create complex Array Structure in PHP

I have to make this kind of structure in array;
We have three ( 3 ) variables which creates this structure:
$numberOfParticipants = 38; // 38 is example
$numberOfParticipantsPerHeat = 8 // 8 is example
$numberOfHeats = 5; // 5 is example
Based on this variables I have this table:
The problem is that, I can't place the ' - ' or null after 31 OR 38. The task is that i have to make the arrays of array "almost equal" like the photo and must depend on the variables above. By the way, after I create the correct list I will slice the array to 5 or 6 or whatever parts I need this is not the problem, the problem is that I have to parse the list like this first. This is what I tried so far:
$calc1 = (int)round($numberOfParticipants * $numberOfParticipantsPerHeat, -1); //First round the numberOfParticipants to closest integer by 10
$readyArr = [];
for ($i = 1; $i <= $calc1; $i++) {
if ($i <= $numberOfParticipants) {
$readyArr[$i] = $i;
} else {
$readyArr[$i] = null;
}
}
The problem with this snippet is that it places the null at the end of the list not after 31, or based on the var.
This is the result I have:
array:40 [▼
1 => 1
2 => 2
3 => 3
4 => 4
5 => 5
6 => 6
7 => 7
8 => 8
9 => 9
10 => 10
11 => 11
12 => 12
13 => 13
14 => 14
15 => 15
16 => 16
17 => 17
18 => 18
19 => 19
20 => 20
21 => 21
22 => 22
23 => 23
24 => 24
25 => 25
26 => 26
27 => 27
28 => 28
29 => 29
30 => 30
31 => 31
32 => 32
33 => 33
34 => 34
35 => 35
36 => 36
37 => 37
38 => 38
39 => null
40 => null
]
The Array after partition I want should be:
array(
0 => array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => 7, 7 => 8,),
1 => array(0 => 9, 1 => 10, 2 => 11, 3 => 12, 4 => 13, 5 => 14, 6 => 15, 7 => 16,),
2 => array(0 => 17, 1 => 18, 2 => 19, 3 => 20, 4 => 21, 5 => 22, 6 => 23, 7 => 24,),
3 => array(0 => 25, 1 => 26, 2 => 27, 3 => 28, 4 => 29, 5 => 30, 6 => 31, 7 => null,),
4 => array(0 => 32, 1 => 33, 2 => 34, 3 => 35, 4 => 36, 5 => 37, 6 => 38, 7 => null,),
);
Every help, every clue will be highly appreciated.
There are two things you need to know about the target structure:
How many players are in the first (which will always be the largest, if only by one) set.
$playersPerHeat = ceil($numberOfParticipants / $numberOfHeats);
// note this replaces your hard-coded $numberOfParticipantsPerHeat
You also need to know how many heats actually have that many, that is how many heats are actually full.
$fullHeats = $numberOfParticipants % $numberOfHeats ?: $numberOfHeats;
// The ?: bit means that if we get zero (ie. all equal heats), then we
// count all the heats instead, since they're all full.
Now it's easy!
$players = range(1,$numberOfParticipants);
$heats = array_merge(
array_chunk(
array_slice($players, 0, $fullHeats * $playersPerHeat),
$playersPerHeat
),
array_chunk(
array_slice($players, $fullHeats * $playersPerHeat),
$playersPerHeat - 1
)
);
That's it! Demo

How can I shift all keys one position forward from a starting and end point in an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I have an array like this:
[
543 => 1,
22 => 3,
65 => 4,
10 => 5,
50 => 6,
]
Now I get a key and a value as input. For example 22 as key and 5 as value.
Now I want to use those two inputs as start and end point in my array and want to shift all keys one forward between those two positions.
[
543 => 1,
22 => 3, ─┐ ┌─ 65 => 3,
65 => 4, ├ Shift all those keys one forward to: ┤ 10 => 4,
10 => 5, ─┘ └─ 22 => 5,
50 => 6,
]
So the expected output would be:
[
543 => 1,
65 => 3,
10 => 4,
22=> 5,
50 => 6,
]
Figure out the start and end offset from your inputs in your array:
$startIndex = array_search(22, array_keys($arr));
$endIndex = array_search(5 , array_values($arr));
//↑ Your input
So for your example array this would look like this:
[
543 => 1, //Offset: 0
22 => 3, //Offset: 1 ← 22 found; offset: 1
65 => 4, //Offset: 2
10 => 5, //Offset: 3 ← 5 found; offset: 3
50 => 6, //Offset: 4
]
Split your array into three parts:
$before = array_slice($arr, 0, $startIndex, true);
$data = array_slice($arr, $startIndex, ($endIndex - $startIndex) + 1, true);
$after = array_slice($arr, $endIndex, null, true);
Visualized this would look like this:
[
543 => 1, → $before; Where you do NOT want to shift your keys
22 => 3, ┐
65 => 4, ├ $data; Where you want to shift your leys
10 => 5, ┘
50 => 6, → $after; Where you do NOT want to shift your keys
]
Rotate the data part keys, just by merging the last key at the start with the other keys at the end:
$keys = array_keys($data);
$keys = array_merge(array_slice($keys, -1), array_slice($keys, 0, -1));
$data = array_combine($keys, $data);
Put it all back together:
$arr = $before + $data + $after;

SQL query how to match 3 out of 4 results

I have searched a number of different items but I have not found any answers. Chances are I just don't know how to word this correctly.
Anyway, I have set up a system in PHP/SQL to allow instantaneous scanning of thousands of results. Each data entry has 4 numbers, and it can easily scan for entries that match all 4 of these numbers. What I am trying to achieve is to have the script search the database for entries that match exactly 3 out of the 4 entries with the other being incorrect, kind of like a lottery.
For example, we have the entries:
Steve - 1, 4, 10, 13
Bill - 3, 4, 10, 13
Tom - 1, 17, 20, 39
Jill - 1, 4, 13, 21
Jane - 5, 10, 13, 18
Now, I would scan based on the results 1, 4, 10, 13, and would like to return the following results, as these matched 3 of the 4 entries:
Bill - 3, 4, 10, 13
Jill - 1, 4, 13, 21
How would I achieve this?
Many thanks
EDIT: Sorry yes the table has the structure
Name - Number1 - Number2 - Number3 - Number4
So yes, stored as separate fields
You can do this by counting the matches and setting this equal to 3:
select t.*
from t
where (val1 in (1, 4, 10, 13) +
val2 in (1, 4, 10, 13) +
val3 in (1, 4, 10, 13) +
val4 in (1, 4, 10, 13)
) = 3;
In MySQL a TRUE boolean expression evaluates to 1. You can add these together to get the number of matches.

Categories