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.
Related
This question already has answers here:
PHP Regex to Find Any Number at the Beginning of String
(2 answers)
Closed 3 years ago.
I have strings with the following pattern: 12345ABCDE6789 where each group of numbers and letters are a variable length. There will only ever be numbers and letters, no special characters.
I need a way to get the numbers up until the first letter, so that the example above would return 12345.
My thought was I could find the string position of the first letter and then trim the string to that position. But I'm having trouble figuring out how to get that index without knowing what the character is. Other solutions I've found have had the knowledge that the first letter would be an "A" where mine could be any letter.
Is there a concise way to do this?
I do not have much experience with regex, but maybe something there would be a better solution to this problem?
<?php
$re = '/^[0-9]+/m';
$str = '12345ABCDE6789';
preg_match($re, $str, $matches);
var_dump($matches[0]); // 12345
https://regexr.com/4mcfr
So long as the number will be <= 2147483647 for 32 bit systems and <= 9223372036854775807 for 64 bit, then the simplest is to cast to an integer and it will truncate letters (anything that will not return a valid integer):
echo (int)"12345ABCDE6789";
Returns 12345
I am looking for a way to extract the first number out of a string. This examples
43
432Phill 21
432hill 21
43#1 Example
43,123 example
should return
43
432
432
43
43,123
I assume it would be possible to use strpos and iterate needle from a-z, A-Z and #. to get all positions and than use the lowest non-zero one to determine the point where number ends. This seems like an overkill. Is there a better way of doing it ?
EDIT: the position is to use substr later. If there is a better way I am down with it.
With a regex, you can extract numbers from a string.
The following statement should do the trick:
preg_match_all('~\d+~', $string, $match);
$match is the array of the numbers contained in $string.
If the number starts at begining you can do this like this:
$number = floatval($string); //it will return float e.g. 432
If you use comma instead of decimal point e.g. 43,123 you must first replace it with dot:
$string = str_replace(',', '.', '43,123 example');
I wrote this regex for checking the input number from my form:
if (!preg_match("/^0\d{10}+|^9\d{9}+/",$_POST['number'])){
echo "Error";
}else{
echo "Ok";
}
this code will check the minimum length but if length is more than 10 or 9 characters, this regex cannot work !
What should I do ? should I check with strlen after Regex or I can limit the maximum length ?
UPDATE:
the string length should be exactly 10 characters if start with 0 and should be exactly 9 characters if start with 9, and should return false on another ways (more or less length, start with different numbers and ...)
Possibly you just want to anchor the string at the end using $. Also can drop the + after the numeric quantifier:
^(?:0\d{9}|9\d{8})$
See test at regex101.com
You can use this regex:
'/^(0\d{9}|9\d{8})$/'
This will allow string length exactly 10 characters if it starts with 0 and should be exactly 9 characters if input starts with 9.
Let say I have to manage 1,000,000 phone numbers which are in 12-digit format. Certainly, they are distinguish. Now I want to assign to each number a shorter string (7 alphanumeric - case sensitive characters) that must be also distinguish. What would be the best solution using Php?
You can use PHP's base_convert() function to change integers into strings.
From integer to a-z0-9 base 36 string: $shortString = base_convert($phoneNumber, 10, 36);
From base 36 string to integer: $phoneNumber = base_convert($shortString, 36, 10);
If that's not short enough and you want to use the full gamut of a-zA-Z0-9 characters, you'll need to use a custom function to convert to base 62. There are some great ones at http://php.net/base_convert.
I need a solution to replace whole string with stars in PHP, for example there are strings like:
test
test123
test1234
And depends on string length, it will replace string with the stars like:
test has 4 characters in length so it will be replaced with 4 stars ****.
test123 has 7 characters in length so it will be replaced with 7 stars *******. And so on...
Is there any good solution for that?
$string = str_repeat('*', strlen($string));
Simply make a new string, consisting of all stars, with length equal to the original.