substr with quotation mark doesn't work properly on php? - php

Why My substr doesn't work properly? Here is my code:
<?php
echo substr("list:[\"76801-2\"", 7,12);//output 76801-2"
?>
the output is: 76801-2" while I want it to be: 76801
I looked on w3 schools example

The second parameter of substr is the length of the new string you want to extract from your current string.
This means that in your case - you take 12 chars starting from the 7th char.
The total length of chars (from position 7) is 7, which is less than 12, so the result will be the entire string.
If you want to take only 5 chars, you can use:
echo substr("list:[\"76801-2\"", 7, 5);//output 76801

Related

PHP preg_match for getting a string out with variable length

I have the following string example: 034a412f500535454e5
Here I would get the 500 out.
The search-string has always 8 digits in front and 8 digits behind. The "500" can have a different lenght of digits (p.ex. 12345).
With a lot of trial end error I found that
preg_match('/(.{8})(.*)(.{13})/', $a, $matches);
It works. But I tink that not the way it is.
I do not understand why the left side has {8} and the right is {13}.
I get my String at following:
$lastInsertedId = 500;
$numArray = str_split(bin2hex(random_bytes(8)), 8);
$newArray = [$numArray[0],$lastInsertedId,$numArray[1]];
$a = vsprintf('%s%s%s',$newArray);
by using:
preg_match('/^.{8}\K.*(?=.{8}$)/', $a, $matches);
the result is 50053545. It will not gives the right value back.
by using:
preg_match('/^.{8}\K.*(?=.{8}$)/', '034a412f500535454e5', $matches);
it gives 500 back
Whats wrong?
gettype($a) gives string back.
I'am on php 8.1.13
If you want to do that with a regex, you can use
^.{8}\K.*(?=.{8}$)
See the regex demo. Use the s flag if the string contains line breaks.
Details
^ - start of string
.{8} - eight chars
\K - omit what was matched so far
.* - any zero or more chars
(?=.{8}$) - a positive lookahead that requires any eight chars followed by the end of string location to appear immediately to the right of the current location.
Also, consider using substr:
$text = '034a412f500535454e5';
echo substr($text, 8, strlen($text)-16); // => 500
vsprintf gives a wrong number of digits back.
strlen('034a412f500535454e5') gives 19
strlen($a) gives 25.
I'am using sprintf instead.

Trying to use substr to get part of string with index but wrong output is coming [duplicate]

This question already has answers here:
substr() return incorrect number of characters
(2 answers)
Closed 3 years ago.
I am trying to use substr to get part of string with index
<?php
echo substr("2019-10-10T02:10:30.413291+05:30",11,16);
?>
but the output is 02:10:30.413291+
what is know the output should have been 02:10
I can't get what am I doing wrong please help
Read the docs. The third parameter is length - how many chars you want to "select" past start.
If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).
echo substr("2019-10-10T02:10:30.413291+05:30", 11, 5);
This outputs 02:10.
The third parameter in substr() is the length of the substring you wish to grab (not the index of the end of the substring):
If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).
In order to grab your desired substring of 2:10, you're looking for a length of 5:
echo substr("2019-10-10T02:10:30.413291+05:30",11,5); // 2:10
This can be seen working here.

substring with last position in php

I want a substring from string in php with starting and last position instead of length.
Since we have function in php substr($str,$start_position,$length);
but I want $last_postion instead of $length because I don't know the length after starting position because it is variable.
e.g $str = october 8, 2012
$str = February 2, 2012
Try using this:
substr($str,$start_position,$last_postion-$start_position);
Well if you don't know the exact length, you need to use strpos first. http://php.net/manual/en/function.strpos.php
You use strpos($the_string, $the_string_to_find) to find the character you're looking for, then you use that returned value in $length for substr.
But as #john said, if you are trying to manipulate a date, or get some value from a date string, it would be a hundred times easier to use strtotime("october 8th, 2012"). You can then format that date however you want, or add / substract from it using a second multiplier.
substr($str, $start_position, $last_postion-$start_position)

PHP: get numeric value in the end of a given formatted string

I "inherited" a buggy PHP page. I'm not an expert of this language but I think I found the origin of the bug. Inside a loop, the page sends a formatted string to the server: the string I found in the HTML page is like this one:
2011-09-19__full_1
so, it seems we have three parts:
a date (0,10);
a string (10,6);
a final number (17,1);
The code the handles this situation is the following:
$datagrid[] = array("date"=>substr($post_array_keys[$i], 0, 10),"post_mode"=>substr($post_array_keys[$i], 10, 6),"class_id"=>substr($post_array_keys[$i], 17, 1),"value"=>$_POST[$post_array_keys[$i]]);
What happens: the final number can contain more than one character, so this piece:
"class_id"=>substr($post_array_keys[$i], 17, 1)
is not correct because it seems to retrieve only one character starting from the 17th (and this seems to cause strange behaviors to the website).
Being the whole number the last part of the string, to get the entire number could I safely change this line this way?
"class_id"=>substr($post_array_keys[$i], 17, strlen($post_array_keys[$i])-17);
If you change the code the way you suggest you would get the numbers at the end starting in position 17. The original code gets only the first digit. Your code would get all the digits.
And it seems you did your homework the line
$datagrid[] = array("date"=>substr($post_array_keys[$i], 0, 10),"post_mode"=>substr($post_array_keys[$i], 10, 6),"class_id"=>substr($post_array_keys[$i], 17, 1),"value"=>$_POST[$post_array_keys[$i]]);
does give you a very good clue of what you should expect in the variable:
first 10 is the date
then you have 6 chars for post_mode
then you have 1 char for class_id
If you also confirmed that sometimes the class_id can be more than 1 char, your suggested change would give you the complete class_id at the end.
Good luck.
you could use
$array = explode("_", $string);
this functions returns an array with the elements in the string delimited by "_".
I suggest this because the double underscore may hide another value that is empty in that particular case.
If it's only the last integer causing trouble, you can use strrchr to get the "tail" of the string, starting with the last '_'.

How can I randomize an entire string of 62 characters?

I have 62 base64 characters that I want to randomize. How can I do this using PHP? The string would be all letters, upper and lower case as well as numbers from 0-9.
The thing that is most important to me is that the entire string be evaluated before a return value is given. In other words, if I request a string of 8 characters in length and my string starts out like:
1234567890ABCDE..... I don't want to get the first 8 numbers randomized. It should randomize the entire string first, then return 8 characters from that.
Try this:
$string = '1234567890ABCDE...';
$string = substr(str_shuffle($string), 0, 8);
str_shuffle randomizes the string, then substr takes the first 8 characters from it.
Take a look at str_shuffle.

Categories