PHP length of String is higher than string? - php

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

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 get group multiple times

I am trying to get a regular expression to get a subgroup everytime it is found. This is my code:
$string2 = 'cabbba';
preg_match_all('#c(a(b)*a)#',$string2,$result3,PREG_SET_ORDER);
var_dump($result3);
My goal is to get 'b' as a captured group each time (so 3 times). This codes outputs the following:
array (size=1)
0 =>
array (size=3)
0 => string 'cabbba' (length=6)
1 => string 'abbba' (length=5)
2 => string 'b' (length=1)
I want it to show 'b' each times it appears, so something like this
array (size=1)
0 =>
array (size=3)
0 => string 'cabbba' (length=6)
1 => string 'abbba' (length=5)
2 => array (size=3)
0 => string 'b' (length 1)
1 => string 'b' (length 1)
2 => string 'b' (length 1)
This is a simplified example, in the real code the subpattern 'b' will be different each time, but it follows the same pattern.
This would be possible only through \G anchor.
(?:ca|\G)(b)(?=b|(a))
DEMO
Did you try using a non-greedy modifier for your b*?
$string2 = 'cabbba';
preg_match_all('#c(a(b)*?a)#', $string2, $result3, PREG_SET_ORDER);
var_dump($result3);
Excuse me if it's not what you asked, I'm not sure I really understood your needs...
UPDATE:
Sorry, previous answer is wrong, please ignore it...
I'm trying to elaborate a right one...
Just trying something like
preg_match_all('#c(a(?:(b{1}))*a)#', $string2, $result3, PREG_SET_ORDER);
but it doesn't work, either... :-(
UPDATE 2:
See Avinash Raj answer, I think it's quite good...

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)

How to convert XML element data into PHP numeric array

I'm not really a web developer and I'm trying to make a web interface for parsing and displaying experimental data, so I apologize if this is a ridiculously easy solution. I'm trying to take data from an XML element and convert it into a PHP numerical array that I can plot using GD. So far, I have the XML creation, XML loading, and GD plotting finished (I think), but what I need is the key to converting the data into a PHP array to finish.
I have an xml file with numeric data stored like this:
<?xml version="1.0" encoding="utf-8" ?>
<NotData>
<Data>0 1 2 3 4 5 6 7 8 9</Data>
</NotData>
I'm using simplexml_load_file to read in the data file. Then I pull out the data like this:
$myData=$xmldata->NotData[0]->Data;
Now I have an object with the data inside, but I'm not sure how to get this out into a numeric array.
Considering you have that string, you first have to load it using SimpleXML :
$str = <<<XML
<?xml version="1.0" encoding="utf-8" ?>
<NotData>
<Data>0 1 2 3 4 5 6 7 8 9</Data>
</NotData>
XML;
$data = simplexml_load_string($str);
I used simplexml_load_string because it was simpler for my test ; of course, you can still use simplexml_load_file, as you are already doing.
Then, to get the field containing the numbers into a variable :
$numbers_string = (string)$data->Data;
var_dump($numbers_string);
Which gets you :
string '0 1 2 3 4 5 6 7 8 9' (length=19)
And, then, you can use the explode function to... well, explode... the string into an array, using a space as a separator :
$numbers_array = explode(' ', $numbers_string);
var_dump($numbers_array);
And you finally get this array :
array
0 => string '0' (length=1)
1 => string '1' (length=1)
2 => string '2' (length=1)
3 => string '3' (length=1)
4 => string '4' (length=1)
5 => string '5' (length=1)
6 => string '6' (length=1)
7 => string '7' (length=1)
8 => string '8' (length=1)
9 => string '9' (length=1)
Which seems to be what you were waiting for ;-)

Categories