Generating 5 digit alphanumeric code with predifined characters in sequence - php
I would like to generate a membership number consisting of alphanumeric characters, removing i o and l to save confusion when typing. to be done in php (also using Laravel 5.7 if that matters - but i feel this is a php question)
If simply using 0-9 the membership number would start at 00001 for the 1st one and the 11th person would have 00011. I would like to use alphanumeric characters from 0-9 + a-z (removing said letters)
0-9 (total 10 characters), abcdefghjkmnpqrstuvwxyz (total 23 characters) - this giving a total of 33 characters in each count cycle (0-10+a-Z). instead of just 10 (0-10)
So the first membership number would still be 00001 where as the 12th would now be 0000a, 14th 0000c and 34th would be 0001a.
To summarize i need a way of defining the characters for counting in a way that can be generated based on the id of a user.
I hope I have explained this well enough.
Assuming that these are the only characters you want to use:
0123456789abcdefghjkmnpqrstuvwxyz
You can use base_convert() and strtr() to translate specific characters of the result to the characters you want.
function mybase33($number) {
return str_pad(strtr(base_convert($number, 10, 33), [
'i' => 'j',
'j' => 'k',
'k' => 'm',
'l' => 'n',
'm' => 'p',
'n' => 'q',
'o' => 'r',
'p' => 's',
'q' => 't',
'r' => 'u',
's' => 'v',
't' => 'w',
'u' => 'x',
'v' => 'y',
'w' => 'z',
]), 5, '0', STR_PAD_LEFT);
}
echo "9 is ".mybase33(9)."\n";
echo "10 is ".mybase33(10)."\n";
echo "12 is ".mybase33(12)."\n";
echo "14 is ".mybase33(14)."\n";
echo "32 is ".mybase33(32)."\n";
echo "33 is ".mybase33(33)."\n";
echo "34 is ".mybase33(34)."\n";
Output:
9 is 00009
10 is 0000a
12 is 0000c
14 is 0000e
32 is 0000z
33 is 00010
34 is 00011
https://3v4l.org/8YtaR
Explanation
The output of base_convert() uses these characters:
0123456789abcdefghijklmnopqrstuvw
The strtr() translates specific characters of that output to:
0123456789abcdefghjkmnpqrstuvwxyz
Related
php base_convert works fine locally, but not on ubuntu 18 LTS server (Laravel 5.7)
Hell internet world I bring another weird and wonderful question to you today. See the code blow, locally (using Laravel's inbuilt php serve functionality, this outputs the following: Which is the desired result [2019-01-31 14:33:07] local.DEBUG: >>: q [2019-01-31 14:33:07] local.DEBUG: --: q [2019-01-31 14:33:07] local.DEBUG: ##: 0000q However, the same files, uploaded to an AWS t2.micro server running Ubuntu 18 LTS - i receive the following: not desired [2019-01-31 14:22:47] local.DEBUG: >>: 14 [2019-01-31 14:22:47] local.DEBUG: --: 14 [2019-01-31 14:22:47] local.DEBUG: ##: 00014 Here is the code snippet to generate an alpha numeric membership number: Thanks to this previous question <?php namespace App\Http\Controllers; use App\Http\Requests\RegisterFormRequest; use App\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use JWTAuth; use PragmaRX\Countries\Package\Countries; use App\Mail\Welcome; use App\cart_storage; use Log; class AuthController extends Controller { private function generateVerification($name, $title) { return md5( $name.substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"), 0, 9).$title ); } private function mybase33($number) { return strtr(base_convert($number, 10, 33), [ 'i' => 'j', 'j' => 'k', 'k' => 'm', 'l' => 'n', 'm' => 'p', 'n' => 'q', 'o' => 'r', 'p' => 's', 'q' => 't', 'r' => 'u', 's' => 'v', 't' => 'w', 'u' => 'x', 'v' => 'y', 'w' => 'z', ]); } public function registerItem(request $request){ $id = (int)Auth::user()->id; $memberCodeShort = $this->mybase33($id); $membershipNumber = str_pad($memberCodeShort, 5, "0", STR_PAD_LEFT); Log::debug('>>: '.$this->mybase33($id)); //$user->membership_number = $this->generateMembershipNumber($id); Log::debug('--: '.$memberCodeShort); Log::debug('##: '.$membershipNumber); } } Any thoughts on what to look at, i'm completely lost. But my gut is telling me there is likely to be something missing or not setup on the server correctly
Just to formalize things from the comments: Your user ID is different on production. 🤯🤬😂
PHP Regex for highlight string [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago. Improve this question I need a script to highlight occurrences of one or more keywords in a text. I have a script in French and I want my script to look for occurrences with special characters and uppercase letters. My URL is (example): .../?q=être And I wish the keywords "être", "Être", "etre" and "Etre" to be highlighted. Is it possible ? (with <mark>...</mark>) Example : Bonjour ceci est un texte avec le verbe <mark>être</mark>, avec la majuscule ça aurait été <mark>Être</mark>, et sans accent <mark>etre</mark>, puis <mark>Etre</mark>. So: Ignore specials characters Ignore uppercase
This sample will match with the required words in your sentence: And I wish the keywords "être", "Être", "etre" and "Etre" to be highlighted. /[êÊeE]tre/
I, i think : $utf8 = array( '/[áàâãªä]/u' => 'a', '/[ÁÀÂÃÄ]/u' => 'A', '/[ÍÌÎÏ]/u' => 'I', '/[íìîï]/u' => 'i', '/[éèêë]/u' => 'e', '/[ÉÈÊË]/u' => 'E', '/[óòôõºö]/u' => 'o', '/[ÓÒÔÕÖ]/u' => 'O', '/[úùûü]/u' => 'u', '/[ÚÙÛÜ]/u' => 'U', '/ç/' => 'c', '/Ç/' => 'C', '/ñ/' => 'n', '/Ñ/' => 'N', '/–/' => '-', // UTF-8 hyphen to "normal" hyphen '/[’‘‹›‚]/u' => ' ', // Literally a single quote '/[“”«»„]/u' => ' ', // Double quote '/ /' => ' ', // nonbreaking space (equiv. to 0x160) ); Thx :) I have found that here : Remove all special characters from a string
Shorten WordPress dates and times
I use human_time_diff Wordpress function on a small area in a thumbnail to show from how much time is it posted, and it appears like: 2 hours , 24 mins ...., and I was wondering if I can make it shorter, like 2h, 24m, I'm sure it could be changed somewhere, can I get some help?
WordPress has a filter available to do this: add_filter( 'human_time_diff', function($since, $diff, $from, $to) { $replace = array( 'min' => 'm', 'mins' => 'm', 'hour' => 'h', 'hours' => 'h', 'day' => 'd', 'days' => 'd', ); return strtr($since, $replace); }, 10, 4 );
Convert charcters from any languages to english characters
I'm searching for a function that can transform or convert any characters (from all languages) to English characters. I saw mb_convert_encoding, iconv('UTF-8', 'ASCII//TRANSLIT', $str) , .... I probably miss something... Example : I want to go from '后杨庄' to 'hou yang zhuang' but iconv echo nothing ;( and mb_convert echo 后杨庄. Is someone knows how to better use mb_convert or iconv ? or is there a better PHP function that can achieve this ? Thanks to answer
you mean romanize (right?), because translation is one thing, and convert encoding another, and romanization a third one Korean Syllable PHP or JavaScript Romanizer (or any other alternative?)
I finally found Transliterator plugin for PHP And with combination with iconv I can remove all accents from all languages $transliterator = Transliterator::create( "Any-Latin; [:Punctuation:] Remove; NFD; NFC;" ); $jStringAccentsRemoved = $transliterator->transliterate($jStringToRemoveAccents); $jStringAccentsRemoved = iconv('UTF-8', 'us-ascii//TRANSLIT', $jStringAccentsRemoved);
* IMPROVED CODE (but PECL intl and PECL translit required) * $tranliterare_Parameters = "Any-Latin; NFD; NFC; NFKD; NFKC; "; $transliterator = Transliterator::create( $tranliterare_Parameters ); $stringToClean = $transliterator->transliterate($stringToClean); $stringToClean = transliterate( $stringToClean, array( 'han_transliterate', 'diacritical_remove' ), 'utf-8', 'utf-8'); $stringToClean = preg_replace('/\p{Mn}/u', '', Normalizer::normalize($stringToClean, Normalizer::FORM_KD)); // this extra portion is from http://d3s.mff.cuni.cz/~holub/sw/phpaccents/ $unwanted_array = array( 'œ' => 'oe' , 'æ' => 'ae' , 'đ' => 'd' , 'ð' => 'd' , 'ø' => 'o' , 'ł' => 'l' , 'ß' => 'ss' , 'Œ' => 'OE' , 'Æ' => 'AE' , 'Đ' => 'D' , 'Ð' => 'D' , 'Ø' => 'O' , 'Ł' => 'L' , 'Ə' => 'E', 'ə' => 'e', 'Ǝ' => 'E', 'ə' => 'e','ı' => 'i','I' => 'i', '`' => '' , '?' => '' , ' ' => ' ' , '"' => '' , "'" => '' , 'À'=>'A','Á'=>'A','Â'=>'A','Ã'=>'A','Ä'=>'A','Å'=>'A','Ç'=>'C','È'=>'E', 'É'=>'E','Ê'=>'E','Ë'=>'E','Ì'=>'I','Í'=>'I','Î'=>'I','Ï'=>'I','Ñ'=>'N', 'Ò'=>'O','Ó'=>'O','Ô'=>'O','Õ'=>'O','Ö'=>'O','Ø'=>'O','Ù'=>'U','Ú'=>'U', 'Û'=>'U','Ü'=>'U','Ý'=>'Y','à'=>'a','á'=>'a','â'=>'a','ã'=>'a','ä'=>'a', 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>'r','s'=>'s','t'=>'t', 'u'=>'u','v'=>'v','w'=>'w','x'=>'x','y'=>'y','z'=>'z' ); $stringToClean = strtr( $stringToClean , $unwanted_array ); The result : FROM 后杨庄 • องค์การโทรศัพท์ร่อนพิบูลย àéç öî-ïüùç ËÀÌ --- ÀØėÿᾜὨζὅБю --- Ⱥⱥ Ƀƀ Ȼȼ Đđ Ɇɇ Ǥǥ Ħħ Ɨɨ Ɉɉ Ꝁꝁ Łł Øø Ᵽᵽ Ɍɍ Ŧŧ Ʉʉ Ɏɏ Ƶƶ TO hou yang zhuang • xngkhkar thorsaphth rxnphibuly aec oi-iuuc EAi --- AOeyEiOzhoBu ---\ Aa Bb Cc Dd Ee GG Hh Ii Jj Kk Ll Oo Pp Rr Tt Uu Yy Zz
get integer / float from string in PHP
I ran into an issue with a data feed I need to import where for some reason the feed producer has decided to provide data that should clearly be either INT or FLOAT as strings-- like this: $CASES_SOLD = "THREE"; $CASES_STOCKED = "FOUR"; Is there a way in PHP to interpret the text string as the actual integer? EDIT: I should be more clear-- I need to have the $cases_sold etc. as an integer-- so I can then manipulate them as digits, store in database as INT, etc.
Use an associative array, for example: $map = array("ONE" => 1, "TWO" => 2, "THREE" => 3, "FOUR" => 4); $CASES_SOLD = $map["THREE"]; // 3
If you are only interested by "converting" one to nine, you may use the following code: $convert = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5, 'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9 ); echo $convert[strtolower($CASES_SOLD)]; // will display 3
If you only need the base 10 numerals, just make a map $numberMap = array( 'ONE' => 1 , 'TWO' => 2 , 'THREE' => 3 // etc.. ); $number = $numberMap[$CASES_SOLD]; // $number == 3' If you need something more complex, like interpreting Four Thousand Two Hundred Fifty Eight into 4258 then you'll need to roll up your sleeves and look at this related question.
Impress your fellow programmers by handling this in a totally obtuse way: <?php $text = 'four'; if(ereg("[[.$text.]]", "0123456789", $m)) { $value = (int) $m[0]; echo $value; } ?>
You need a list of numbers in english and then replace to string, but, you should play with 'thousand' and 'million' clause where must check if after string 'thousend-three' and remove integer from string. You should play with this function and try change if-else and add some functionality for good conversion: I'm writing now a simple code for basic, but you know others what should change, play! Look at million, thousand and string AND, it should be change if no in string like '1345'. Than replace with str_replace each of them separaterly and join them to integer. function conv($string) { $conv = array( 'ONE' => 1, 'TWO' => 2, 'THREE' => 3, 'FOUR' => 4, 'FIVE' => 5, 'SIX' => 6, 'SEVEN' => 7, 'EIGHT' => 8, 'NINE' => 9, 'TEN' => 10, 'ELEVEN' => 11, 'TWELVE' => 12, 'THIRTEEN' => 13, 'FOURTEEN' => 14, 'FIFTEEN' => 15, 'SIXTEEN' => 16, 'SEVENTEEN' => 17, 'EIGHTEEN' => 18, 'NINETEEN' => 19, 'TWENTY' => 20, 'THIRTY' => 30, 'FORTY' => 40, 'FIFTY' => 50, 'SIXTY' => 60, 'SEVENTY' => 70, 'EIGTHY' => 80, 'NINETY' => 90, 'HUNDRED' => 00, 'AND' => '', 'THOUSAND' => 000 'MILLION' => 000000, ); if (stristr('-', $string)) { $val = explode('-', $string); #hardcode some programming logic for checkers if thousands, should if trim zero or not, check if another values foreach ($conv as $conv_k => $conv_v) { $string[] = str_replace($conv_k, $conv_v, $string); } return join($string); } else { foreach ($conv as $conv_k => $conv_v) { $string[] = str_replace($conv_k, $conv_v, $string); } return join($string); } }
Basically what you want is to write a parser for the formal grammar that represents written numbers (up to some finite upper bound). Depending on how high you need to go, the parser could be as trivial as $numbers = ('zero', 'one', 'two', 'three'); $input = 'TWO'; $result = array_search(strtolower($input), $numbers); ...or as involved as a full-blown parser generated by a tool as ANTLR. Since you probably only need to process relatively small numbers, the most practical solution might be to manually hand-code a small parser. You can take a look here for the ready-made grammar and implement it in PHP.
This is similar to Converting words to numbers in PHP PHP doesn't have built in conversion functionality. You'd have to build your own logic based on switch statements or otherwise. Or use an existing library like: http://www.phpclasses.org/package/7082-PHP-Convert-a-string-of-English-words-to-numbers.html