UTF8 correct string length with vardump - php

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.

Related

PHP length of String is higher than string?

i would like to import an csv file and I have got a problem to compare the result with a constant.
my file is looking something like this:
1;1;23
1;2,11
...
When I dumped the first row I got this result:
array (size=3)
0 => string '1' (length=4)
1 => string '1' (length=1)
2 => string '23' (length=2)
How the hell could be the result string '1' = length 4
I also returned the length of the string with strlen and also trim the string but i got everytime the result 4. Btw I use the PHP version 7.4 with xampp

Can not match the last group of numbers using php preg_match()

preg_match_all("/(\d{12})
(?:,|$)/","111762396541,561572500056,561729950637,561135281443",$matches);
var_dump($mathes):
array (size=2)
0 =>
array (size=4)
0 => string '561762396543,' (length=13)
1 => string '561572500056,' (length=13)
2 => string '561729950637,' (length=13)
3 => string '561135281443' (length=12)
1 =>
array (size=4)
0 => string '561762396543' (length=12)
1 => string '561572500056' (length=12)
2 => string '561729950637' (length=12)
3 => string '561135281443' (length=12)
But I want the $matches like this:
array (size=4)
0 => string '561762396543,' (length=13)
1 => string '561572500056,' (length=13)
2 => string '561729950637,' (length=13)
3 => string '561135281443' (length=12)
I wanna match groups of numbers(each has 12 digits) and a suffix comma if there is one.The exeption is the last group of numbers,it doesnt have to match a comma,cause it reaches the end of the line.
Try this instead:
preg_match_all("/(\d{12}(?:,|$))/","111762396541,561572500056,561729950637,561135281443",$matches);
When the $ is inside your character range brackets [ ] it is looking for the $ characters not the end-of-line.
EDIT: If you want to include the comma in your matches, then just use the above code sample and look at $matches[0].
If you wanted an easier syntax that matches any sort of word boundary, the \b will match commas and end-of-line, too:
preg_match_all("/(\d{12}\b)/","111762396541,561572500056,561729950637,561135281443",$matches);

preg_match Regex Matching Full String

I have a simple regex, but it's matching more than I want...
Basically, I'm trying to match certain operators (eg. > < != =) followed by a string.
Regex:
/^(<=|>=|<>|!=|=|<|>)(.*)/
Example subject:
>42
What I'm getting:
array (size=3)
0 => string '>42' (length=3)
1 => string '>' (length=1)
2 => string '42' (length=2)
What I'm trying to get:
array (size=2)
0 => string '>' (length=1)
1 => string '42' (length=2)
What I don't understand is that my regex works perfectly on Regex101
Edit: To clarify, how can I get rid of the full string match?
Your answer is correct.Group(0) is the whole match.Group(1) if first group and group(2) is the second group.
You are getting all 3 groups \0, \1, and '\2'. see the group matching at the bottom of the page
assuming your matches are in $matches you can run array_shift($matches) to remove the '\0' match if you wish.

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)

Counting matches from preg_match_all

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

Categories