Counting matches from preg_match_all - php

I've been searching for similar question but haven't found no one with similar problem or possible solution.
I have preg_match_all function which finds me 5 matches and returns them in array. How can I count strings that are in array ?
preg_match_all("/\)\">(.*?)<\/a>/s",$value,$counter);
here is var_dump(counter[1]):
array (size=1)
0 => string 'word1' (length=..)
array (size=1)
0 => string 'word2' (length=..)
array (size=1)
0 => string 'word3' (length=..)
array (size=1)
0 => string 'word4' (length=..)
array (size=1)
0 => string 'word5' (length=..)
As you seen it returns me 5 matches, but i cant print that number neither with array_count_values, count()... I've tried imploding then to get strings but count() wont work either.
count($counter[1]) returns me ->
int 1
int 1
int 1
int 1
int 1
I need to get number of matches and I dont have any more ideas, so anything is very welcome.
Update: Here is var_dump($value);
string '<h2>word1</h2><div class="show"><p class="nums"><span class="phone">03 839 11 00</span></p>' (length=170)
string '<h2>word2</h2><div class="show"><p class="nums"><span class="phone">03 839 12 00</span><span class="fax">03 839 12 18</span><span class="email email-mod"><a title="info#golte.si" href="mailto:info#golte.si"><img alt='info#golte.si' src='Txt2Image.ashx?AAARAAcASQAAAB0AEgAGADMAEwAGAB8AAAAMAF0ABwAAAFUAGQAGABcAEQBUAAAAAAAQAB8AEQANAFUAEgAGAB0AAABUADIABgAAABIAGABSAEIAQABSAAMADABSAFAARQAtAEcARQBeADYATwALABwAGAANAEMARABZAEMARQBdADIARQA=' /></a></s'... (length=938)`
string '<h2>word3</h2><div class="show"><p class="nums"><span class="phone">03 839 12 12</span></p>' (length=162)
string '<h2>word4</h2><div class="show"><p class="nums"><span class="phone">03 839 12 14</span></p>' (length=168)
string '<h2>word5</h2><div class="show"><p class="nums"><span class="phone">03 839 61 28</span></p>' (length=162)

preg_match_all itself returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.
$number = preg_match_all("/\)\">(.*?)<\/a>/s",$value,$counter);

<?php
/* function strlen1($val)
{
return strlen($val);
} */
//get the each value count
$b = array_map('strlen', $counter);
print_r($b);

Related

UTF8 correct string length with vardump

How can I make var_dump like output from an array that manages string characters length well, that is the same counts with and without accents?
/var/www/test.php:4:
array (size=2)
0 => string 'qwertzuiop' (length=10)
1 => string 'qwértzúíóp' (length=14)
See mb_strlen(): http://php.net/manual/en/function.mb-strlen.php
This should return a length of 10 for 'qwértzúíóp'.
var_dump() will always return a length of 14 bytes for 'qwértzúíóp' because that is the actual size.

preg_match_all for numbers in certain parts of string

Babylon 5 Season 4 Episode 13 Rumors Bargains and Lies 45
how i can extract numbers that comes after Season and the numbers that comes only after episode not any number after that. in the above example. i would want only 4 and 13 numbers using php using preg_match thanks in advance
You can do something like this:
$str = 'Babylon 5 Season 4 Episode 13 Rumors Bargains and Lies 45';
if (preg_match_all("/(Season|Episode) (\d.)/", $str, $matches)) {
var_dump($matches);
}
And it will output:
array (size=3)
0 =>
array (size=2)
0 => string 'Season 4 ' (length=9)
1 => string 'Episode 13' (length=10)
1 =>
array (size=2)
0 => string 'Season' (length=6)
1 => string 'Episode' (length=7)
2 =>
array (size=2)
0 => string '4 ' (length=2)
1 => string '13' (length=2)
You can have your Season and Episode values in an array m:
preg_match_all('/.*Season\s+(?<season>\d+)\s+Episode\s+(?<episode>\d+)/', $str, $m);
print 'Season: ' . $m['season'][0] . "\n";
print 'Episode: ' . $m['episode'][0] . "\n";
You can use regex look behind to capture numbers.
(?<=Season)\s*?\d+|(?<=Episode)\s*?\d+
It should capture 4 and 13.
See https://regex101.com/r/JVOUOw/2

array_rand returning same value

I am having a problem returning random array keys if the specified number of entries is the same as the number of items in the array.
$rdm = array_rand($similar_product_array, 4);
will always return key values 0, 1, 2, 3 if there is 4 items in the array.
for example:
// Items in array
array (size=4)
0 => string 'Batman Heroes Edition Nendoroid' (length=31)
1 => string 'Oberyn' (length=6)
2 => string 'White Walker' (length=12)
3 => string 'Avengers Age of Ultron Hulk' (length=27)
// "randomly" generated array keys is always 0 , 1, 2, 3
array (size=4)
0 => int 0
1 => int 1
2 => int 2
3 => int 3
however, if i have:
$rdm = array_rand($similar_product_array, 3);
// Returns randomly as expected
array (size=3)
0 => int 0
1 => int 2
2 => int 3
it will return randomly generated keys as it should.
What could i be doing wrong here?
You misunderstood purpose of array_rand() function, it is supposed to give you random entries from array, but not in random order. That means that if you are asking for 4 random items from array with 4 items, it will always return all the items (in the original order).
If you just need to change randomly the order of array entries, use shuffle() function, for example in this way:
$array_copy = $array;
shuffle($array_copy);
$rdm = array_rand($array_copy, <how_many_you_need>);

how to find out number from given string using preg_match?

if (
preg_match("/^bundle id/", trim($rows[$key])) &&
preg_match('/\d/', trim($rows[$key]), $temp))
.
bundle id 1 mode active
bundle id 99 mode active
bundle id 999 mode active
how to find out 1,99 and 999 in given preg_match expression.
Your second preg_match needs to become preg_match_all, and the regex needs to look for \d+, which is one or more numerical digits:
preg_match_all('/\d+/', trim($rows[$key]), $temp))
$temp will now contain an array of values:
array (size=1)
0 =>
array (size=3)
0 => string '1' (length=1)
1 => string '99' (length=2)
2 => string '999' (length=3)
Edit
The above answer was on the basis that the sample string is one line.
If each line represents a different string, then all you need to do is alter the regex to\d+:
if(preg_match("/^bundle id/", trim($rows[$key])) &&
preg_match('/\d+/', trim($rows[$key]), $temp))
I'd do:
preg_match("/^bundle id\s*(\d+)/", trim($rows[$key]), $match)
Then the results are in $match[1]

Regex Extracting After the Match

preg_match('/\$(\d+\.\d+)/',$message,$keywords);
dd($keywords);
Hi , have got a few questions
1) Is it possible to detect/extract the text after the regular expression? eg I'm trying to detect $1.20 possible to detect the text after it eg per hour , /hr , per hr, / hour.
1.1) Maybe like Extract 20 characters after the match
1.2) Possible to know the position of the match if i cant extract?
$100000/hour test test test
Extract test test tst
1) Put everything you want to extract in the regex, like this:
preg_match('#\$(\d+\.\d+)(\s+per hour|\s*/hr|\s+per hr|\s*/hour)?#',$message,$keywords);
You'll get the amount in $keywords[1] and the other piece of text in $keywords[2];
1.1) Use /\$(\d+\.\d+)(.{,20})/ to get at most 20 characters in the second match (if you remove the comma it will match only if after the amount there are at least 20 characters).
1.2) Use the $flags parameter of preg_match(): preg_match('/\$(\d+\.\d+)/',$message,$keywords,PREG_OFFSET_CAPTURE);. Check print_r($keywords) to see how the matched values and their offsets are returned
You probably need to find all the appearances. In this case use preg_match_all().
Try this:
$re = '~\$(\d+\.?\d+)/?(\w+)?~m';
$str = "$100000/hour\n$100.2000/min";
preg_match_all($re, $str, $matches);
var_dump($matches);
Demo on regex101
Output
array (size=3)
0 =>
array (size=2)
0 => string '$100000/hour' (length=12)
1 => string '$100.2000/min' (length=13)
1 =>
array (size=2)
0 => string '100000' (length=6)
1 => string '100.2000' (length=8)
2 =>
array (size=2)
0 => string 'hour' (length=4)
1 => string 'min' (length=3)

Categories