Skip to content

Commit 54900d2

Browse files
committed
allow urlencode(int)
in strict mode it's kindof annoying to have to cast-to-string when you have a string|int variable you need to urlencode. There is no ambiguity as to how to urlencode integers, so it shouldn't be a problem. "TypeError: urlencode(): Argument php#1 ($string) must be of type string, int given" Has happened to me enough times now that I bothered to make PR to allow it. Fwiw the integer path could be micro-optimized to bypass php_raw_url_encode() entirely, at least when the integer is not negative, but for brewity, I didn't bother micro-optimizing it.
1 parent 7e1d035 commit 54900d2

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3715,7 +3715,7 @@ function parse_url(string $url, int $component = -1): int|string|array|null|fals
37153715
* @compile-time-eval
37163716
* @refcount 1
37173717
*/
3718-
function urlencode(string $string): string {}
3718+
function urlencode(string|int $string): string {}
37193719

37203720
/**
37213721
* @compile-time-eval
@@ -3727,7 +3727,7 @@ function urldecode(string $string): string {}
37273727
* @compile-time-eval
37283728
* @refcount 1
37293729
*/
3730-
function rawurlencode(string $string): string {}
3730+
function rawurlencode(string|int $string): string {}
37313731

37323732
/**
37333733
* @compile-time-eval

ext/standard/basic_functions_arginfo.h

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/url.c

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,28 @@ PHPAPI zend_string *php_url_encode(char const *s, size_t len)
552552
/* {{{ URL-encodes string */
553553
PHP_FUNCTION(urlencode)
554554
{
555-
zend_string *in_str;
555+
zend_string *in_str = NULL;
556+
zend_long in_long;
557+
zend_string *tmpstr = NULL;
558+
zend_string *encoded_str;
556559

557-
ZEND_PARSE_PARAMETERS_START(1, 1)
558-
Z_PARAM_STR(in_str)
559-
ZEND_PARSE_PARAMETERS_END();
560+
ZEND_PARSE_PARAMETERS_START(1, 1)
561+
Z_PARAM_STR_OR_LONG(in_str, in_long)
562+
ZEND_PARSE_PARAMETERS_END();
563+
564+
if (in_str == NULL) {
565+
/* Input is an integer */
566+
in_str = tmpstr = zend_long_to_str(in_long);
567+
}
560568

561-
RETURN_STR(php_url_encode(ZSTR_VAL(in_str), ZSTR_LEN(in_str)));
569+
encoded_str = php_url_encode(ZSTR_VAL(in_str), ZSTR_LEN(in_str));
570+
571+
/* Release the temporary string if we allocated one */
572+
if (tmpstr != NULL) {
573+
zend_string_release(tmpstr);
574+
}
575+
576+
RETURN_STR(encoded_str);
562577
}
563578
/* }}} */
564579

@@ -614,13 +629,29 @@ PHPAPI zend_string *php_raw_url_encode(char const *s, size_t len)
614629
/* {{{ URL-encodes string */
615630
PHP_FUNCTION(rawurlencode)
616631
{
617-
zend_string *in_str;
632+
zend_string *in_str = NULL;
633+
zend_long in_long;
634+
zend_string *tmpstr = NULL;
635+
zend_string *encoded_str;
618636

619-
ZEND_PARSE_PARAMETERS_START(1, 1)
620-
Z_PARAM_STR(in_str)
621-
ZEND_PARSE_PARAMETERS_END();
637+
ZEND_PARSE_PARAMETERS_START(1, 1)
638+
Z_PARAM_STR_OR_LONG(in_str, in_long)
639+
ZEND_PARSE_PARAMETERS_END();
640+
641+
if (in_str == NULL) {
642+
/* Input is an integer */
643+
in_str = tmpstr = zend_long_to_str(in_long);
644+
}
645+
646+
encoded_str = php_raw_url_encode(ZSTR_VAL(in_str), ZSTR_LEN(in_str));
647+
648+
649+
/* Release the temporary string if we allocated one */
650+
if (tmpstr != NULL) {
651+
zend_string_release(tmpstr);
652+
}
622653

623-
RETURN_STR(php_raw_url_encode(ZSTR_VAL(in_str), ZSTR_LEN(in_str)));
654+
RETURN_STR(encoded_str);
624655
}
625656
/* }}} */
626657

tests/strings/001.phpt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
String functions
33
--FILE--
44
<?php
5-
5+
declare(strict_types=1);
66
echo "Testing strtok: ";
77

88
$str = "testing 1/2\\3";
@@ -202,6 +202,11 @@ if (strlen($ui1) == strlen($ui2) && strlen($ui1) == $len && $ui1 != $ui2) {
202202
echo("failed!\n");
203203
}
204204

205+
echo "testing (raw)urlencode(int)\n";
206+
var_dump(urlencode(1));
207+
var_dump(rawurlencode(1));
208+
209+
205210
?>
206211
--EXPECT--
207212
Testing strtok: passed
@@ -221,3 +226,6 @@ Testing addslashes: passed
221226
Testing stripslashes: passed
222227
Testing uniqid(true): passed
223228
Testing uniqid(false): passed
229+
testing (raw)urlencode(int)
230+
string(1) "1"
231+
string(1) "1"

0 commit comments

Comments
 (0)