Setting Substr to larger than string length - php

This is more of underlying PHP execution question.
What are the positives or negatives to the two following methods for limiting string size?
function max_length($string, $length) {
return (strlen($string) > $length)?substr($string, 0, $length):$string;
}
substr($string, 0, $length);
The case I am worried about is when the string is smaller than the length requested. Can it cause buffer overflow errors? Extra whitespace? Repeated characters?
The only thing I do know is that speed-wise, substr is at least 2x faster than the custom function provided.

As you can see in http://us2.php.net/substr
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 ).
That means you won't get any extra characters, just the ones you're asking for.

No, nothing to worry about. If the specified length argument is higher than the length of the string - the whole string, and nothing else - will be returned. So just go with:
$maxString = substr($string, 0, $length);

Related

php break string into smaller parts based on string, not length

I have read up on the php functions wordwrap and chunk_split but I can't figure out how to break down a string into smaller chunks when there are no physical breaks in the string.
I have a URL-encoded string:
%5B%7B%22partNumber%22%3A%2243160-1104%22%7D%2C%7B%22partNumber%22%3A%2242410-6170%22%7D%2C%7B%22partNumber%22%3A%2222-10-2021%22%7D%2C%7B%22partNumber%22%3A%2255091-0674%22%7D%2C%7B%22partNumber%22%3A%2243160-0106%22%7D%2C%7B%22partNumber%22%3A%2287832-1420%22%7D%2C%7B%22partNumber%22%3A%2273415-1001%22%7D%2C%7B%22partNumber%22%3A%2253627-1274%22%7D%2C%7B%22partNumber%22%3A%2243650-0510%22%7D%5D
of a bunch of part numbers I'm feeding into an API. This API can only take 500 characters at a time before it returns a false to me, so I need to break my string down to UNDER 500 characters, but still be a complete, searchable string.
Meaning - however it's broken down, each iteration of this new string needs to be
under 500 characters
end with B%22, so that the next iteration of the string starts with
partNumber%22
I'm not sure how I would accomplish this using the wordwrap + explode method as I've only ever used this to break a string by length. Is there a function similar to this that I can use where I can specify an exact string to break at after so many characters?
use explode.
$apiStrings = explode("B%22", $string);
foreach($apiStrings as $apiString)
{
//Do request
}

How to get a cryptographically strong integer from 0-X in PHP?

I want to generate random alphanumeric strings in PHP. They will be used in places where the strength of random numbers is important (publicly visible IDs in URLs and the like).
As I understand, in PHP the main source of cryptographically strong randomness is openssl_random_pseudo_bytes(). This however returns an array of bytes, not alphanumeric characters.
To convert them to alphanumerics I could either hash them (which would produce a longer-than-necessary string of a limited set of hex characters), or base64_encode() them (which would produce a string with +, / and = in it - not alphanumerics).
So I think that instead I could use the random bytes as a source of entropy and generated my own string consisting only of the characters 0-9a-zA-Z.
The problem then becomes - how to translate from 256 distinct values (one byte of input) to 62 distinct value (one character of output). And in a way, that all 62 characters are equally as likely. (Otherwise there will be 8 characters that appear more often than the rest).
Or perhaps I should use another approach entirely? I would like my string to be as short as possible (say, 20 characters or so - shorter URLs are better) and consist only of alphanumeric characters (so that it doesn't need to be specially escaped anywhere).
You can implement your own base64 encoding, sort of. If you can allow two specific symbols - these can be anything, for example . and -, it doesn't really matter. It can even be a space for one of them. In any case, what you would do is this:
$alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
// using . and - for the two symbols here
$input = [123,193,21,13]; // whatever your input it, I'm assuming an array of bytes
$output = "";
foreach($input as $byte) {
$output .= $alphabet[$byte%64];
}
Assuming random input, all characters have equal probability of appearing.
That being said, if you can't allow anything except pure alphanumeric, cut the symbols from the $alphabet and use %62 instead of %64. While this does mean you have a small bias towards the chracters 0 through 7, I don't think it's significant enough to worry about.
I found this function on php.net in the user comments.
function crypto_rand($min,$max) {
$range = $max - $min;
if ($range == 0) return $min; // not so random...
$length = (int) (log($range,2) / 8) + 1;
return $min + (hexdec(bin2hex(openssl_random_pseudo_bytes($length,$s))) % $range);
}
Then do something like
for($i=0; $i<20; $i++)
{
$string.= chr(crypto_rand(1,26)+96); //or +64 for upper case
}
Or similar.
note: THIS IS WRONG! I leave this attempted answer for reference only.
(31 * 256) % 62 = 0
For each output alphanumeric character, generate 31 random values. Sum these 31 values and take the modulo 62.
Kind of brutal, but this is the only "mathematicaly correct" option I can think of :)

PHP - Cut last N chars in string faster than substr($s, 0, -$N)

I have a script trimming strings this way billions of times.
$s = substr($s, 0, -$n);
Is there a way to do it faster an without reassigning the string?
By definition strings are not mutable in PHP. To "cut" a string, you'll have to create a new string based on the original string, making it necessary to reassign it. The code you have is probably already the most minimalist way to do it.

PHP String Pattern Task

A simple problem.
I have the following string "48063974806397"
You will notice that this is just "4806397" repeated twice.
I need a way to recognize the repeat point, and just get the first instance of the pattern. E.g final return should just be "4806397".
(The length of the first number will not always be the same.)
I wanted to return this a variable in php.
How could I do this?
Thanks
If it's always just a string duplicated twice, then it's as simple as just taking the first half of the string:
$halfstring = substr($string, 0, strlen($string) / 2);
Use strlen() to get the length of the string, and divide that by 2. Then use substr() to just get the first half.
If that's always a number, math helps:
$halfStr = $n / (pow(10, strlen($n) / 2) + 1);

php substr and text manipulation

I would like to take this data:
questionX
Where X is any number from zero to infinity.
And subtract the text question from the field, and save just the number.
I was toying with substr($data, 0, 8) but haven't gotten it to work right, can somebody please give me a suggestion? Thanks so much.
The manual for substring http://php.net/manual/en/function.substr.php
indicates that the function takes three arguments: the string, the start, and the length. Length is optional. If omitted, substr will return the rest of the string starting from 'start'. One way to get your X value would be to do something like this:
$theNumber = substr($data, 8);
It look like you don't' really understand what substr does. Substr does not 'subtract' part of the string from itself. It returns the part of the string that you specify with the start and length paramaters
Try this
$number = str_replace("question", "", $string);

Categories