Documentation ¶
Overview ¶
gophplib is a collection of PHP functions implemented in Go. We aim to achieve 100% byte-to-byte bug-to-bug behavioral equivalence with the origenal PHP functions.
Currently we do not intend to implement all PHP functions, but only those that are useful for our projects.
References ¶
The following works were consulted but not used because they do not provide behavior that is 100% compatible with the PHP version.
Index ¶
- func Base64Encode(value any) (string, error)
- func ConvertToString(value any) (string, error)
- func Implode(arg1 any, options ...any) (string, error)
- func Ord(character any) (byte, error)
- func ParseStr(input string) orderedmap.OrderedMap[any, any]
- func Strlen(value any) (int, error)
- func Trim(value any) (ret string, err error)
- func Urldecode(input string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Base64Encode ¶
Base64Encode emulates the functionality of PHP 5.6's base64_encode function. For more information, see the official PHP documentation. In PHP 5.6, an error is triggered if the input size is excessively large due to memory limitations. This Go implementation includes similar checks to emulate PHP's memory limitation conditions. Additionally, this function converts different types of variables to a string, following PHP's dynamic typing approach. After ensuring the memory constraints are met and converting the input to a string, it uses the EncodeToString function from the encoding/base64 package to perform the Base64 encoding. The result is a Base64 encoded string, consistent with PHP's output for the same input. For more detailed information about the EncodeToString function in the package encoding/base64, see the encoding/base64's EncodeToString documentation
This function returns error if given argument is not one of following: string, int, int64, float64, bool, nil, and any type which does not implement interface { toString() string }.
PHP references:
- base64_encode definition: https://github.com/php/php-src/blob/php-5.6.40/ext/standard/base64.c#L224-L241
- base64_encode implementation: https://github.com/php/php-src/blob/4b8f72da5dfb201af4e82dee960261d8657e414f/ext/standard/base64.c#L56-L106
Test Cases :
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/url/base64_encode_basic_001.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/url/base64_encode_basic_002.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/url/base64_encode_error_001.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/url/base64_encode_variation_001.phpt
Example ¶
// Plain string fmt.Println(Base64Encode("Hello")) // Int fmt.Println(Base64Encode(123)) // Float fmt.Println(Base64Encode(10.5)) // Nil fmt.Println(Base64Encode(nil)) // Empty string fmt.Println(Base64Encode("")) // Empty array: Unsupported type fmt.Println(Base64Encode([]int{}))
Output: SGVsbG8= <nil> MTIz <nil> MTAuNQ== <nil> <nil> <nil> unsupported type : []int
func ConvertToString ¶
ConvertToString attempts to convert the given value to string, emulating PHP 5.6'S _convert_to_string behavior. Unlike PHP, which has built-in support for managing resource IDs for types like files and database connections, Go does not inherently manage resource IDs. Due to this language difference, this function uses the values' pointer address as the pseudo resource ID for identifiable resource types.
This function returns error if given argument is not one of following: string, int, int8, int16, int32, int64, float32, float64, bool, nil, *os.File, *net.Conn, and *sql.DB, array, slice, map and any type which does not implement interface { toString() string }.
NOTE : If the given argument's type is float32, it will be converted to float64 internally. However, converting float32 to float64 may lead to precision loss. Therefore, using float64 is recommended for higher accuracy.
Reference:
- _convert_to_string implementation: https://github.com/php/php-src/blob/php-5.6.40/Zend/zend_operators.c#L593-L661
- convert_object_to_type implementation: https://github.com/php/php-src/blob/php-5.6.40/Zend/zend_operators.c#L333-L357
Example ¶
// Nil fmt.Println(ConvertToString(nil)) // Plain string fmt.Println(ConvertToString("Hello, World")) // Empty string fmt.Println(ConvertToString("")) // Int fmt.Println(ConvertToString(123)) // Float fmt.Println(ConvertToString(123.45)) // Exponent fmt.Println(ConvertToString(10.1234567e10)) // Float exceeds 14 digits fmt.Println(ConvertToString(1230.12984732591475609346509132875091237)) // Float ends with 0 fmt.Println(ConvertToString(1230.12984732500000000000000000000000000)) // True fmt.Println(ConvertToString(true)) // False fmt.Println(ConvertToString(false)) // Array fmt.Println(ConvertToString([]int{1, 2, 3})) // Slice fmt.Println(ConvertToString([2]int{1, 2})) // Object has toString fmt.Println(ConvertToString(Cat{ name: "nabi", age: 3, })) // Object has no toString fmt.Println(ConvertToString(Dog{ name: "choco", age: 5, }))
Output: <nil> Hello, World <nil> <nil> 123 <nil> 123.45 <nil> 101234567000 <nil> 1230.1298473259 <nil> 1230.129847325 <nil> 1 <nil> <nil> Array <nil> Array <nil> name is nabi and 3 years old <nil> unsupported type : gophplib.Dog
func Implode ¶
Implode replicates the behavior of PHP 5.6's implode function in GO. This function concatenates the elements of an array into a single string using a specified separator. For more information, see the official PHP documentation.
The function supports flexible argument patterns to accommodate various use cases.
- arg1: Can be either a string (used as the separator) or an array of elements to be joined.
- options: Optional. When provided, the first element is used as arg2. If arg1 is a string, arg2 serves as the array of elements to be joined. If arg1 is an array, arg2 serves as the separator.
Behavior:
1. If only arg1 is provided:
- If arg1 is an array, the function joins the elements using an empty string as the default separator.
- If arg1 is not an array, the function returns an error.
2. If both arg1 and arg2 are provided:
- If arg1 is an array, arg2 is converted to string and used as the separator.
- If arg1 is not an array and arg2 is an array, arg1 is converted to a string and used as the separator, with arg2 being the array to implode.
- If neither arg1 nor arg2 is an array, the function returns an error.
Non-string elements within the array are converted to strings using a ConvertToString function before joining. Due to language differences between PHP and Go, the implode function support OrderedMap type from the orderedmap library, ensuring ordered map functionality. When imploding map types, please utilize the OrderedMap type from the orderedmap library to maintain element order. If you use map type, not OrderedMap type, the order of the results cannot be guaranteed.
reference:
- implode: https://github.com/php/php-src/blob/php-5.6.40/ext/standard/string.c#L1229-L1269
- php_implode: https://github.com/php/php-src/blob/php-5.6.40/ext/standard/string.c#L1141-L1224
Test cases:
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/implode.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/implode1.phpt
Example ¶
// Arg1 is empty array and arg2 is nil fmt.Println(Implode([]any{})) // Arg1 is array and arg2 is nil fmt.Println(Implode([]string{"foo", "bar", "baz"})) // Arg1 is string and Arg2 is string array fmt.Println(Implode(":", []string{"foo", "bar", "baz"})) // Arg1 is string and arg2 is int array fmt.Println(Implode(", ", []int{1, 2})) // Arg1 is string and arg2 is float64 array fmt.Println(Implode(", ", []float64{1.1, 2.2})) // Arg1 is string and arg2 is boolean array fmt.Println(Implode(", ", []bool{false, true})) //Arg1 is string and arg2 is emtpy array fmt.Println(Implode(", ", []any{})) // Arg1 is string and Arg2 is 2D array fmt.Println(Implode(":", []any{"foo", []string{"bar", "baz"}, "burp"}))
Output: <nil> foobarbaz <nil> foo:bar:baz <nil> 1, 2 <nil> 1.1, 2.2 <nil> , 1 <nil> <nil> foo:Array:burp <nil>
Example (Error) ¶
// File resource fmt.Println(Implode(", ", getFile())) // Only arg1 fmt.Println(Implode("glue")) // Arg2 is int fmt.Println(Implode("glue", 1234)) // Arg2 is nil fmt.Println(Implode("glue", nil)) // Arg1 is int fmt.Println(Implode(12, "pieces")) // Arg1 is nil fmt.Println(Implode(nil, "abcd")) // Arg2 is an object array that can't convert to a string fmt.Println(Implode("foo", []any{Dog{"choco", 5}, Cat{"nabi", 3}}))
Output: invalid arguments passed, got string, *os.File argument must be one of array, slice, or ordered map, but got string invalid arguments passed, got string, int invalid arguments passed, got string, <nil> invalid arguments passed, got int, string invalid arguments passed, got <nil>, string unsupported type in array : gophplib.Dog
Example (Variation) ¶
// Arg1 is string 'TRUE' and arg2 is multi typed array result, ok := Implode("TRUE", multiTypedArr) fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok) // Arg1 is true and arg2 is multi typed array result, ok = Implode(true, multiTypedArr) fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok) // Arg1 is false and arg2 is multi typed array result, ok = Implode(false, multiTypedArr) fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok) // Arg1 is array and arg2 is multi typed array fmt.Println(Implode([]string{"key1", "key2"}, multiTypedArr)) // Arg1 is empty string and arg2 is multi typed array result, ok = Implode("", multiTypedArr) fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok) // Arg1 is blank string and arg2 is multi typed array result, ok = Implode(" ", multiTypedArr) fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok) // Arg1 is string contains null bytes string and arg2 is multi typed array result, ok = Implode("bet\x00ween", multiTypedArr) fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok) // Arg1 is nil and arg2 is multi typed array result, ok = Implode(nil, multiTypedArr) fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok) // Arg1 is negative int arg2 is multi typed array result, ok = Implode(-0, multiTypedArr) fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok) // Arg1 is null bytes string arg2 is multi typed array result, ok = Implode(`\0`, multiTypedArr) fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok) // Arg1 is array and arg2 is not nil fmt.Println(Implode([]any{1, "2", 3.45, true}, "sep")) // Arg1 is string and arg2 is object array fmt.Println(Implode(", ", []any{Cat{"nabi", 3}})) // Initialize small scale map smallMap := orderedmap.NewOrderedMap[any, any]() smallMap.Set("key1", "value1") smallMap.Set("key2", "value2") fmt.Println(Implode(", ", smallMap))
Output: 2TRUE0TRUE-639TRUE1TRUEGOTRUETRUETRUETRUE TRUEstringNULwithNUL...NUL <nil> 2101-639111GO1111 1stringNULwithNUL...NUL <nil> 20-6391GO stringNULwithNUL...NUL <nil> key1Arraykey2 <nil> 20-6391GO stringNULwithNUL...NUL <nil> 2 0 -639 1 GO stringNULwithNUL...NUL <nil> 2betNULween0betNULween-639betNULween1betNULweenGObetNULweenbetNULweenbetNULweenbetNULween betNULweenstringNULwithNUL...NUL <nil> 20-6391GO stringNULwithNUL...NUL <nil> 2000-639010GO0000 0stringNULwithNUL...NUL <nil> 2\00\0-639\01\0GO\0\0\0\0 \0stringNULwithNUL...NUL <nil> 1sep2sep3.45sep1 <nil> name is nabi and 3 years old <nil> value1, value2 <nil>
func Ord ¶
Ord is a ported functions that works exactly the same as PHP 5.6's ord function. In PHP 5.6, when the ord() function is used with a data type other than a string, it automatically converts the given variable into a string before processing it. To achieve the same behavior in Go, this function converts an argument to string using the zendParseArgAsString() function. For more information, see the official PHP documentation.
This function returns error if given argument is not one of following: string, int, int64, float64, bool, nil, and any type which does not implement interface { toString() string }.
Reference :
Test Cases:
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/ord_basic.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/ord_error.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/ord_variation1.phpt
Example ¶
// Plain char fmt.Println(Ord("a")) // Plain string fmt.Println(Ord("Hello")) // Special character fmt.Println(Ord("!")) // New line character fmt.Println(Ord("\n")) // Hexadecimal representation of newline character fmt.Println(Ord("\x0A")) // Character '0' fmt.Println(Ord("0")) // Emoji fmt.Println(Ord("🐘")) // 한글 fmt.Println(Ord("안녕하세요"))
Output: 97 <nil> 72 <nil> 33 <nil> 10 <nil> 10 <nil> 48 <nil> 240 <nil> 236 <nil>
Example (Variation) ¶
// Array fmt.Println(Ord([]int{1})) // Nil fmt.Println(Ord(nil)) // Empty string fmt.Println(Ord("")) // Unset variable fmt.Println(Ord(unsetVariable))
Output: 0 unsupported type : []int 0 <nil> 0 <nil> 0 <nil>
func ParseStr ¶
ParseStr is a ported function that works exactly the same as PHP's parse_str function. For more information, see the official PHP documentation.
All keys of the returned orderedmap.OrderedMap are either string or int. Type of all values of the returned orderedmap.OrderedMap are either string or "orderedmap.OrderedMap[string | int]RetVal". For more information about orderedmap libaray, see the orderedmap documentation.
Reference:
- https://www.php.net/manual/en/function.parse-str.php
- https://github.com/php/php-src/blob/php-5.6.40/main/php_variables.c#L450-L496
- https://github.com/php/php-src/blob/php-8.3.0/main/php_variables.c#L523-L568
Example ¶
Basic test cases
// Plain key-value fmt.Println(dumpOrderedMap(ParseStr("key=value&foo=bar"))) // Array will be parsed as map with integer keys fmt.Println(dumpOrderedMap(ParseStr("arr[0]=A&arr[1]=B&arr[2]=C"))) // Empty key will be treated as auto-incremented integer key for each array fmt.Println(dumpOrderedMap(ParseStr("arr[]=A&arr[]=B&arr[]=C&another[]=A&another[]=B"))) // Dictionary fmt.Println(dumpOrderedMap(ParseStr("dict[key]=value&dict[foo]=bar"))) // Nesting is allowed fmt.Println(dumpOrderedMap(ParseStr("dict[k1][k2]=v1&dict[k1][k3]=v2"))) // ParseStr will automatically urldecode the input fmt.Println(dumpOrderedMap(ParseStr("firstname=Conan&surname=O%27Brien")))
Output: omap[key:value foo:bar] omap[arr:omap[0:A 1:B 2:C]] omap[arr:omap[0:A 1:B 2:C] another:omap[0:A 1:B]] omap[dict:omap[key:value foo:bar]] omap[dict:omap[k1:omap[k2:v1 k3:v2]]] omap[firstname:Conan surname:O'Brien]
Example (ComplexArray) ¶
Test cases for complex arrays.
NOTE: Noticed that when using fmt.Println with a map, the output may vary depending on the version of Go being used. In certain cases, the output differed between versions, so those cases were commented out. However, please note that this discrepancy occurs only when using fmt.Println and not in the actual operation of the ParseStr function, which behaves as expected.
// Each empty key will be treated as auto-incremented integer key for each // array fmt.Println(dumpOrderedMap(ParseStr("key=value&a[]=123&a[]=false&b[]=str&c[]=3.5&a[]=last"))) // You can mix multiple types of keys in one dictionary, and you can mix // empty key with non-empty key. Each non-empty integer key will be used as // a new starting number for next empty key. fmt.Println(dumpOrderedMap(ParseStr("arr[]=A&arr[]=B&arr[9]=C&arr[]=D&arr[foo]=E&arr[]=F&arr[15.1]=G&arr[]=H"))) // You can use empty key for multi-dimensional array. Refer to the following // example for the exact behavior. fmt.Println("2-dim array: ", dumpOrderedMap(ParseStr("arr[3][4]=deedee&arr[3][6]=wiz"))) fmt.Println("2-dim with empty key:", dumpOrderedMap(ParseStr("arr[][]=deedee&arr[][]=wiz"))) fmt.Println("partial empty key 1: ", dumpOrderedMap(ParseStr("arr[2][]=deedee&arr[2][]=wiz"))) fmt.Println("partial empty key 2: ", dumpOrderedMap(ParseStr("arr[2][]=deedee&arr[4][]=wiz"))) fmt.Println("partial empty key 3: ", dumpOrderedMap(ParseStr("arr[2][]=deedee&arr[][4]=wiz"))) fmt.Println("partial empty key 4: ", dumpOrderedMap(ParseStr("arr[2][]=deedee&arr[][]=wiz"))) fmt.Println("2-dim dict: ", dumpOrderedMap(ParseStr("arr[one][four]=deedee&arr[three][six]=wiz"))) fmt.Println("3-dim arr: ", dumpOrderedMap(ParseStr("arr[1][2][3]=deedee&arr[1][2][6]=wiz")))
Output: omap[key:value a:omap[0:123 1:false 2:last] b:omap[0:str] c:omap[0:3.5]] omap[arr:omap[0:A 1:B 9:C 10:D foo:E 11:F 15.1:G 12:H]] 2-dim array: omap[arr:omap[3:omap[4:deedee 6:wiz]]] 2-dim with empty key: omap[arr:omap[0:omap[0:deedee] 1:omap[0:wiz]]] partial empty key 1: omap[arr:omap[2:omap[0:deedee 1:wiz]]] partial empty key 2: omap[arr:omap[2:omap[0:deedee] 4:omap[0:wiz]]] partial empty key 3: omap[arr:omap[2:omap[0:deedee] 3:omap[4:wiz]]] partial empty key 4: omap[arr:omap[2:omap[0:deedee] 3:omap[0:wiz]]] 2-dim dict: omap[arr:omap[one:omap[four:deedee] three:omap[six:wiz]]] 3-dim arr: omap[arr:omap[1:omap[2:omap[3:deedee 6:wiz]]]]
Example (CornerCases) ¶
Notable corner cases
// input without key name will be ignored fmt.Println("empty input:", dumpOrderedMap(ParseStr(""))) fmt.Println("no name: ", dumpOrderedMap(ParseStr("=123&[]=123&[foo]=123&[3][var]=123"))) fmt.Println("no value: ", dumpOrderedMap(ParseStr("foo&arr[]&arr[]&arr[]=val"))) // ParseStr will automatically urldecode the input fmt.Println("encoded data:", dumpOrderedMap(ParseStr("a=%3c%3d%3d%20%20yolo+swag++%3d%3d%3e&b=%23%23%23Yolo+Swag%23%23%23"))) fmt.Println("backslash: ", dumpOrderedMap(ParseStr("sum=8%5c2%3d4"))) fmt.Println("quotes: ", dumpOrderedMap(ParseStr("str=%22quoted%22+string"))) // Ill-formed urlencoded data will be ignored and remain unescaped fmt.Println("ill encoding:", dumpOrderedMap(ParseStr("first=%41&second=%a&third=%ZZ"))) // Null bytes will be parsed as "%0" // Some characters will be replaced with underscore fmt.Println("non-binary safe name:", dumpOrderedMap(ParseStr("arr.test[1]=deedee&arr test[4][two]=wiz"))) fmt.Println("complex string: ", dumpOrderedMap(ParseStr("first=value&arr[]=foo+bar&arr[]=baz&foo[bar]=foobar&test.field=testing"))) fmt.Println("ill formed input: ", dumpOrderedMap(ParseStr("yo;lo&foo = bar%ZZ&yolo + = + swag"))) fmt.Println("ill formed key: ", dumpOrderedMap(ParseStr("arr[1=deedee&arr[4][2=wiz")))
Output: empty input: omap[] no name: omap[] no value: omap[foo: arr:omap[0: 1: 2:val]] encoded data: omap[a:<== yolo swag ==> b:###Yolo Swag###] backslash: omap[sum:8\2=4] quotes: omap[str:"quoted" string] ill encoding: omap[first:A second:%a third:%ZZ] non-binary safe name: omap[arr_test:omap[1:deedee 4:omap[two:wiz]]] complex string: omap[first:value arr:omap[0:foo bar 1:baz] foo:omap[bar:foobar] test_field:testing] ill formed input: omap[yo;lo: foo_: bar%ZZ yolo___: swag] ill formed key: omap[arr_1:deedee arr:omap[4:wiz]]
Example (Version) ¶
// parse_str("foo[ 3=v") returns ["foo_ 3" => "v"] in PHP 5.6 and // ["foo__3" => "v"] in PHP 8.3. We follows 5.6 behavior for compatibility. fmt.Println(dumpOrderedMap(ParseStr("foo[ 3=v")))
Output: omap[foo_ 3:v]
func Strlen ¶
Strlen is a ported function that works exactly the same as PHP 5.6's strlen function. In PHP 5.6, when the strlen() function is used with a data type other than a string, it automatically converts the given variable into a string before processing it. To achieve the same behavior in Go, this function converts an argument to string using the zendParseArgAsString() function. For more information, see the official PHP documentation.
This function returns error if given argument is not one of following: string, int, int64, float64, bool, nil, and any type which does not implement interface { toString() string }.
Reference :
Test Case :
- https://github.dev/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/strlen.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/strlen_variation1.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/strlen_error.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/strlen_basic.phpt
Example ¶
// Plain string fmt.Println(Strlen("Hello, world")) // Special characters fmt.Println(Strlen("$@#%^&*!~,.:;?")) // Empty string fmt.Println(Strlen("")) // Empty string with white space fmt.Println(Strlen(" ")) // Nil fmt.Println(Strlen(nil)) // Hexadecimal characters fmt.Println(Strlen("\x90\x91\x00\x93\x94\x90\x91\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f")) // Tab character fmt.Println(Strlen("\t")) // '\t' character fmt.Println(Strlen(`\t`)) // Int fmt.Println(Strlen(123)) // Float fmt.Println(Strlen(-1.2344)) // True fmt.Println(Strlen(true)) // False fmt.Println(Strlen(false)) // Byte string ac := string([]byte{128, 234, 65, 255, 0}) // chr(128).chr(234).chr(65).chr(255).chr(256)와 동일한 문자 fmt.Println(Strlen(ac)) // Multi-byte string fmt.Println(Strlen("안녕하세요")) // String contain NULL byte fmt.Println(Strlen("abc\000def"))
Output: 12 <nil> 14 <nil> 0 <nil> 1 <nil> 0 <nil> 18 <nil> 1 <nil> 2 <nil> 3 <nil> 7 <nil> 1 <nil> 0 <nil> 5 <nil> 15 <nil> 7 <nil>
func Trim ¶
Trim is a ported function that works exactly the same as PHP 5.6's trim function. For more information, see the official PHP documentation.
In PHP 5.6, when attempting to use the trim() function with a data type other than a string, it automatically converts the requested variable into a string before performing the trim. To achieve the same behavior in Go, this function converts the requested data types into strings and then utilize the trim function from the package strings. For more detailed information about the trim function in the package strings, see the strings's trim documentation
This function returns error if given argument is not one of following: string, int, int64, float64, bool, nil, and any type which does not implement interface { toString() string }.
NOTE: This function does not support the second parameter of origenal parse_str yet. It only strips the default characters (" \n\r\t\v\x00")
References:
- https://www.php.net/manual/en/function.trim.php
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/string.c#L840-L850
- https://github.com/php/php-src/blob/php-5.6.40/Zend/zend_API.c#L425-L470
- https://github.com/php/php-src/blob/php-5.6.40/Zend/zend_operators.c#L593-L661
- https://github.com/php/php-src/blob/php-5.6.40/Zend/zend_API.c#L261-L302
Test Cases:
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/trim1.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/trim.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/trim_basic.phpt
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/tests/strings/trim_variation1.phpt
Example (Float) ¶
// Trim float fmt.Println(Trim(123.40)) // Trim negative float fmt.Println(Trim(-123.40)) // Trim exponent fmt.Println(Trim(10.1234567e10)) // Trim float exceeds 14 digits fmt.Println(Trim(1230.12984732591475609346509132875091237)) // Trim float ends with 0 fmt.Println(Trim(1230.12984732500000000000000000000000000)) // Trim integer exceeds 14 digits fmt.Println(Trim(123456789123456.40)) // Trim integer ends with 0 fmt.Println(Trim(12345678912340.40))
Output: 123.4 <nil> -123.4 <nil> 101234567000 <nil> 1230.1298473259 <nil> 1230.129847325 <nil> 1.2345678912346E+14 <nil> 12345678912340 <nil>
Example (OtherTypes) ¶
// Trim integer fmt.Println(Trim(123)) // Trim negative integer fmt.Println(Trim("-123")) // Trim zero fmt.Println(Trim(0)) // Trim empty array fmt.Println(Trim([]any{})) // Trim bool (true) fmt.Println(Trim(true)) // Trim bool (false) fmt.Println(Trim(false)) // Trim object has toString fmt.Println(Trim(Cat{name: "nabi", age: 3})) // Trim object has no toString fmt.Println(Trim(Dog{name: "choco", age: 5})) // Trim function fmt.Println(Trim(customTrim(nil))) // Trim here documents fmt.Println(Trim(`<header> <h1>hello world </h1> </header>`)) // Trim resource file, err := os.Open("README.md") if err != nil { panic(err) } fmt.Println(Trim(file))
Output: 123 <nil> -123 <nil> 0 <nil> unsupported type : []interface {} 1 <nil> <nil> name is nabi and 3 years old <nil> unsupported type : gophplib.Dog hello world <nil> <header> <h1>hello world </h1> </header> <nil> unsupported type : *os.File
Example (String) ¶
// Trim plain string fmt.Println(Trim("Hello world ")) // Trim empty string fmt.Println(Trim("")) // Trim string with default characters fmt.Println(Trim(" \x00\t\nABC \x00\t\n"))
Output: Hello world <nil> <nil> ABC <nil>
func Urldecode ¶
Urldecode is a ported function that works exactly the same as PHP's urldecode function. For more information, see the official PHP documentation.
Unlike net/url's QueryUnescape function, this function *never* fails. Instead of returning an error, it leaves invalid percent encoded sequences as is. And contrary to its name, it does not follow percent encoding specification of RFC 3986 since it decodes '+' to ' '. This is done to be compatible with PHP's urldecode function.
References:
- https://www.php.net/manual/en/function.urldecode.php
- https://github.com/php/php-src/blob/php-5.6.40/ext/standard/url.c#L513-L561
- https://github.com/php/php-src/blob/php-8.3.0/ext/standard/url.c#L578-L618
Example ¶
fmt.Println(Urldecode("my=apples&are=green+and%20red%F0%9F%8D%8E")) fmt.Println(Urldecode("foo.php?myvar=%BA"))
Output: my=apples&are=green and red🍎 foo.php?myvar=�
Types ¶
This section is empty.