PHP has a feature where you can use increment operators on strings. It behaves similarly to an odometer, where once you reach the end of a range, it "rolls over".
<?php
$str = 'zy';
$str++;
echo "$str\n"; // zz
$str++;
echo "$str\n"; // aaa
Just curious where in the PHP source code this is. I often look in the source code at functions/extensions, but something like this I have no idea where to look.
A link to the file using their web based SVN would be awesome.
The implementation for this operator is conveniently located in zend_operators.c, in a function that's even more conveniently called increment_string():
static void increment_string(zval *str) /* {{{ */
{
int carry=0;
int pos=Z_STRLEN_P(str)-1;
char *s=Z_STRVAL_P(str);
char *t;
int last=0; /* Shut up the compiler warning */
int ch;
if (Z_STRLEN_P(str) == 0) {
STR_FREE(Z_STRVAL_P(str));
Z_STRVAL_P(str) = estrndup("1", sizeof("1")-1);
Z_STRLEN_P(str) = 1;
return;
}
if (IS_INTERNED(s)) {
s = (char*) emalloc(Z_STRLEN_P(str) + 1);
memcpy(s, Z_STRVAL_P(str), Z_STRLEN_P(str) + 1);
Z_STRVAL_P(str) = s;
}
while (pos >= 0) {
ch = s[pos];
if (ch >= 'a' && ch <= 'z') {
if (ch == 'z') {
s[pos] = 'a';
carry=1;
} else {
s[pos]++;
carry=0;
}
last=LOWER_CASE;
} else if (ch >= 'A' && ch <= 'Z') {
if (ch == 'Z') {
s[pos] = 'A';
carry=1;
} else {
s[pos]++;
carry=0;
}
last=UPPER_CASE;
} else if (ch >= '0' && ch <= '9') {
if (ch == '9') {
s[pos] = '0';
carry=1;
} else {
s[pos]++;
carry=0;
}
last = NUMERIC;
} else {
carry=0;
break;
}
if (carry == 0) {
break;
}
pos--;
}
if (carry) {
t = (char *) emalloc(Z_STRLEN_P(str)+1+1);
memcpy(t+1, Z_STRVAL_P(str), Z_STRLEN_P(str));
Z_STRLEN_P(str)++;
t[Z_STRLEN_P(str)] = '\0';
switch (last) {
case NUMERIC:
t[0] = '1';
break;
case UPPER_CASE:
t[0] = 'A';
break;
case LOWER_CASE:
t[0] = 'a';
break;
}
STR_FREE(Z_STRVAL_P(str));
Z_STRVAL_P(str) = t;
}
}
/* }}} */
Related
I want to make an auto-increment alphanumeric and this is just for output
if i press a button , there is an output AA001 and then i press a button again there is an output AA002
This doesn't use any 'clever' tricks but is shorter and easier to understand and tweak. It also uses numbers so it doesn't grow the length of the string as quickly. It increments from 0-9 then a-z, but it is intended that you should feed in 'a' to start with. Output always starts with an alphabet character, as long as your input also starts with an alphabet character, so it can be used as PHP variable names
var nextVarName = function(str) {
// Position of char to change.
var change = str.length - 1;
// The value of that char.
var change_char = str[change];
// Number of zeros to append when flipping.
var zeros = 0;
// Iterate backwards while there's a z (flipping).
while (change_char == 'z') {
// Increase the length of appended zeros
zeros++;
// Move the char to change back.
change_char = str[--change];
}
if (change_char == undefined) {
// Full flip - string increases in length.
str = 'a' + Array(str.length + 1).join("0");
} else {
// Normal increment with partial flip and 9->a handling.
str = str.substr(0, change)
+ (change_char == '9' ? 'a' : String.fromCharCode(str.charCodeAt(change) + 1))
+ Array(zeros + 1).join('0');
}
return str;
};
var vname = 'a';
for (var i = 0; i < 5; i++) {
vname = nextVarName(vname);
console.log(vname);
}
The results will be as follows:
z ---> a0
9z ---> a0 (unexpected input)
ab1zde ---> ab1zdf
abcdzz ---> abce00
zzzzz ---> a00000
abcyzz ---> abcz00
9000 ---> 9001 (unexpected input)
https://jsfiddle.net/e20kfvky/
The schedule of how many variables (that start with an alphabet char) are created of each length is as follows:
1: 26, 2: 936, 3: 33696, 4: 1213056 ... n: 36 ^ n - 10 * 36 ^ (n - 1)
The following code will generate a token that you can advance by one. It uses some small classes to make it modular. The function you attach to the button will call the function next on the token as shown in the very bottom.
//Element in a big token
function Increment(startval, endval) {
this.start = startval.charCodeAt(0);
this.cur = this.start;
this.end = endval.charCodeAt(0);
//Returns the current value
this.get = function() {
if (this.cur <= this.end) {
return String.fromCharCode(this.cur);
} else {
return null;
}
}
//Advances the value we will show
this.next = function() {
this.cur += 1;
return this.get();
}
//Reset it to the beginning
this.reset = function() {
this.cur = this.start;
}
}
function Token() {
this.token = [
new Increment("A", "Z"),
new Increment("A", "Z"),
new Increment("0", "9"),
new Increment("0", "9"),
new Increment("0", "9")
];
this.get = function() {
return this.token.map(function(cur) {
return cur.get();
}).join("");
}
this.next = function() {
//From the back to the front
for (var i = this.token.length - 1; i >= 0; i--) {
if (this.token[i].next() == null) {
//If we exhausted all possible values, continue
this.token[i].reset();
} else {
//Until we advance one that has still values left
break;
}
}
return this.get();
}
}
//Now we are just showing off...
var a = new Token();
for (var i = 0; i < 15; i++) {
console.log(a.next());
}
const getNextAlphaString = function (str) {
"use strict";
str=str.toUpperCase();
let chars;
chars = str.split("");
const strLen = str.length;
let continueIncermenting = true;
for (let i = strLen - 1; i >= 0; i = i - 1) {
let asciiVal;
asciiVal = chars[i].charCodeAt(0);
if (isNaN(asciiVal)) {
return str;
}
if (continueIncermenting === true) {
if (asciiVal >= 48 && asciiVal < 57) {
chars[i] = String.fromCharCode(asciiVal + 1);
continueIncermenting = false;
break;
} else if (asciiVal == 57) {
chars[i] = '0';
continueIncermenting = true;
}
if (asciiVal >= 65 && asciiVal < 90) {
chars[i] = String.fromCharCode(asciiVal + 1);
continueIncermenting = false;
break;
} else if (asciiVal == 90) {
chars[i] = String.fromCharCode(65);
continueIncermenting = true;
}
} else {
if (asciiVal == 90) {
continueIncermenting = true;
chars[i] = String.fromCharCode(65);
}
if (asciiVal == 57) {
continueIncermenting = true;
chars[i] = '0';
}
}
}
if (continueIncermenting === true) {
let firstAcii = chars[0].charCodeAt(0);
if (isNaN(firstAcii)) {
return str;
}
if ((firstAcii >= 65 && firstAcii <= 90) || (firstAcii >= 97 && firstAcii <= 122)) {
return 'A' + chars.join('').toUpperCase();
}
if (firstAcii >= 48 && firstAcii <= 57) {
return '0' + chars.join('').toUpperCase();
}
}
return chars.join('').toUpperCase();
};
The results will be as follows:
ab1zde ---> AB1ZDF
abcdzz ---> ABCEAA
zzzzz ---> AAAAAA
abcyzz ---> ABCZAA
9000 ---> 9001
Is there an easy way in PHP to figure out the source code/content of a built in function?
Say for example I want to know what base64_decode() actually does to the given encoded base64 string to convert it to a plain text. How can I achieve this?
You can browse the source code of PHP here
In your case base64_decode is implemented here (PHP 5.6.0)
Note: This code is in C since that's what PHP is written in. In fact all built-in functions and extensions of PHP are written in C.
PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length, int *ret_length, zend_bool strict) /* {{{ */
{
const unsigned char *current = str;
int ch, i = 0, j = 0, k;
/* this sucks for threaded environments */
unsigned char *result;
result = (unsigned char *)safe_emalloc(length, 1, 1);
/* run through the whole string, converting as we go */
while ((ch = *current++) != '\0' && length-- > 0) {
if (ch == base64_pad) {
if (*current != '=' && ((i % 4) == 1 || (strict && length > 0))) {
if ((i % 4) != 1) {
while (isspace(*(++current))) {
continue;
}
if (*current == '\0') {
continue;
}
}
efree(result);
return NULL;
}
continue;
}
ch = base64_reverse_table[ch];
if ((!strict && ch < 0) || ch == -1) { /* a space or some other separator character, we simply skip over */
continue;
} else if (ch == -2) {
efree(result);
return NULL;
}
switch(i % 4) {
case 0:
result[j] = ch << 2;
break;
case 1:
result[j++] |= ch >> 4;
result[j] = (ch & 0x0f) << 4;
break;
case 2:
result[j++] |= ch >>2;
result[j] = (ch & 0x03) << 6;
break;
case 3:
result[j++] |= ch;
break;
}
i++;
}
k = j;
/* mop things up if we ended on a boundary */
if (ch == base64_pad) {
switch(i % 4) {
case 1:
efree(result);
return NULL;
case 2:
k++;
case 3:
result[k] = 0;
}
}
if(ret_length) {
*ret_length = j;
}
result[j] = '\0';
return result;
}
So, i was challenged by a coworker to build a random poker hand generator. At the time, all I had was my phone that I could run php with.
So, after some tinkering and lots of searching, i came up with this:
<?php
$cards;
$card = array();
function GetCard($getSuit, $getNumber){
for($s=0; $s < 4; $s++){
for($n=0; $n < 13; $n++){
if($s == 0){
//Clubs
$suits[$s] = "-C";
} else if($s == 1){
//Hearts
$suits[$s] = "-H";
} else if($s == 2){
//Spades
$suits[$s] = "-S";
} else if($s == 3){
//Diamonds
$suits[$s] = "-D";
}
if($n == 0){
$num[$n] = "A";
} else if($n == 10){
$num[$n] = "J";
} else if($n == 11){
$num[$n] = "Q";
} else if($n == 12){
$num[$n] = "K";
} else {
$num[$n] = $n+1;
}
$cards[$s][$n] = $num[$n].$suits[$s];
}
}
return $cards[$getSuit][$getNumber];
}
function GetRandomPokerHand(){
$i = 0;
while($i < 5){
mt_srand();
$rs = mt_rand(0,3);
$rn = mt_rand(0,12);
$randomCard = GetCard($rs,$rn);
if(!in_array($randomCard,$card)){
$card[i] = GetCard($rs,$rn);
echo $card[i];
echo " ";
$i++;
}else{
echo " found ";
}
}
}
GetRandomPokerHand();
?>
I get the hand, however... every so often in_array fails and I get the same card twice. i added the "else" statement with "found" to see if it was finding the duplicate at all. It did and echos "found", and occasionally it STILL displays a duplicate.
So I decided to try the same code (well roughly the same) when i got home with c++: (Using "switch" instead of "if" was because of preference and switch statements are too much trouble on a phone)
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
using namespace std;
string suits[4];
string num[13];
string cards[4][13];
string GetCard(int getSuit, int getNumber){
for (int s = 0; s < 4; s++){
for (int n = 0; n < 13; n++){
switch (s){
case 0:
suits[s] = "-C";
break;
case 1:
suits[s] = "-H";
break;
case 2:
suits[s] = "-S";
break;
case 3:
suits[s] = "-D";
break;
}
switch (n){
case 0:
num[n] = "A";
break;
case 10:
num[n] = "J";
break;
case 11:
num[n] = "Q";
break;
case 12:
num[n] = "K";
break;
default:
num[n] = to_string(n + 1);
break;
}
cards[s][n] = num[n] + suits[s];
}
}
string card = { cards[getSuit][getNumber] };
return card;
}
bool in_array(const string &value, const vector<string> &array){
return find(array.begin(), array.end(), value) != array.end();
}
void GetRanddomPokerHand(){
int hand = 0;
srand(time(NULL));
while (hand < 5){
int suit = rand() % 4;
int value = rand() % 13;
string randomCard = GetCard(suit, value);
vector<string> card = { "", "", "", "", "" };
if (!in_array(randomCard, card)){
card[hand] = randomCard;
cout << card[hand] << endl;
hand++;
}else{
cout << "found";
}
}
}
int main(){
GetRanddomPokerHand();
char stop;
cin >> stop; // yes, i know this isn't necessary in VE2013
return 0;
}
Same problem. I can't seem to figure out why the duplicates are being printed in either case.
Any ideas?
In the c++ one you have this line inside your loop
vector<string> card = { "", "", "", "", "" };
That is creating a fresh blank hand each time, so it never will have duplicates.
$card[i] need to be $card[$i]
your $card array always has only 1 element which is last card cause of that mistake ;)
I am wondering if there is a rot5 function for PHP equalivant too
function rot5(str) {
var s = [];
for (i = 0; i < str.length; i ++)
{
idx = str.charCodeAt(i);
if ((idx >= 48) && (idx <= 57))
{
if (idx <= 52)
{
s[i] = String.fromCharCode(((idx + 5)));
}
else
{
s[i] = String.fromCharCode(((idx - 5)));
}
}
else
{
s[i] = String.fromCharCode(idx);
}
}
return s.join('');
}
In javascript? If not then how can I do fromCharCode and charCodeAt in PHP?
Here you got generic strrot with default to strrot5
<?php
function strrot($str, $n=5){
$replace = [];
for($i=0;$i<26;$i++){
$replace[chr(65+$i)] = chr(65+(($i+$n)%26));
$replace[chr(97+$i)] = chr(97+(($i+$n)%26));
}
return strtr($str, $replace);
}
You can use 'ord' for 'charCodeAt'.
ord($yourstring[$yourindex])
You can use 'chr' for 'fromCharCode'
chr($yourcharcode)
I have this PHP code which is supposed to increase a URL shortener mask on each new entry.
My problem is that it dosen't append a new char when it hits the last one (z).
(I know incrementing is a safety issue since you can guess earlier entries, but this is not a problem in this instance)
If i add 00, it can figure out 01 and so on... but is there a simple fix to why it won't do it on its own?
(The param is the last entry)
<?php
class shortener
{
public function ShortURL($str = null)
{
if (!is_null($str))
{
for($i = (strlen($str) - 1);$i >= 0;$i--)
{
if($str[$i] != 'Z')
{
$str[$i] = $this->_increase($str[$i]);
#var_dump($str[$i]);
break;
}
else
{
$str[$i] = '0';
if($i == 0)
{
$str = '0'.$str;
}
}
}
return $str;
}
else {
return '0';
}
}
private function _increase($letter)
{
//Lowercase: 97 - 122
//Uppercase: 65 - 90
// 0 - 9 : 48 - 57
$ord = ord($letter);
if($ord == 122)
{
$ord = 65;
}
elseif ($ord == 57)
{
$ord = 97;
}
else
{
$ord++;
}
return chr($ord);
}
}
?>
Effectively, all you are doing is encoding a number into Base62. So if we take the string, decode it into base 10, increment it, and reencode it into Base62, it will be much easier to know what we are doing, and the length of the string will take care of itself.
class shortener
{
public function ShortURL($str = null)
{
if ($str==null) return 0;
$int_val = $this->toBase10($str);
$int_val++;
return $this->toBase62($int_val);
}
public function toBase62($num, $b=62) {
$base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$r = $num % $b ;
$res = $base[$r];
$q = floor($num/$b);
while ($q) {
$r = $q % $b;
$q =floor($q/$b);
$res = $base[$r].$res;
}
return $res;
}
function toBase10( $num, $b=62) {
$base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$limit = strlen($num);
$res=strpos($base,$num[0]);
for($i=1;$i<$limit;$i++) {
$res = $b * $res + strpos($base,$num[$i]);
}
return $res;
}
}