diff options
Diffstat (limited to 'ext/standard/string.c')
-rw-r--r-- | ext/standard/string.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/ext/standard/string.c b/ext/standard/string.c index 09d6f71d82..5f2c0538cd 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -3933,6 +3933,39 @@ PHP_FUNCTION(str_rot13) /* }}} */ +static int php_string_shuffle(const void *a, const void *b TSRMLS_DC) +{ + long rnd; + rnd = php_rand(TSRMLS_C); + if (rnd % 3) + return 1; + else if (rnd % 5) + return 0; + else + return -1; +} + +/* {{{ proto string str_shuffle(string str) + Shuffles string. One permutation of all possible is created */ +PHP_FUNCTION(str_shuffle) +{ + /* Note : by using current php_string_shuffle for string */ + /* with 6 chars (6! permutations) about 2/3 of them are */ + /* computed for 6! calls for the function. So it isn't so */ + /* unique. The ratio is the same for other lengths. */ + char *str; + int i, str_len; + + i = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) { + RETURN_FALSE; + } + zend_qsort((void *)str, str_len, sizeof(char), php_string_shuffle TSRMLS_CC); + RETURN_STRINGL(str, str_len, 1); +} +/* }}} */ + + #if HAVE_STRFMON /* {{{ proto string money_format(string format , float value) Convert monetary value(s) to string */ |