imartingraham / php

Implements some PHP functions by Golang. This library has no third-party dependency.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP functions implemented by Golang

GoDoc Build Status codecov Release

This package implements some PHP functions by Golang. Please note that it's impossible to have 100% the same behaviour between PHP and Golang functions because their mechanisms are quite different.

Minimum go version requirement:

OS Go version
Linux 1.9
OSX 1.12
Windows 1.13

Date/Time Functions

PHP function Golang function
checkdate Checkdate
date Date
strtotime Strtotime
time Time
microtime Microtime
date_add DateAdd
date_create_from_format DateCreateFromFormat
date_create DateCreate
date_date_set DateDateSet
date_default_timezone_get DateDefaultTimezoneGet
date_default_timezone_set DateDefaultTimezoneSet
date_diff DateDiff
date_format DateFormat
date_interval_create_from_date_string DateIntervalCreateFromDateString
date_isodate_set DateISODateSet
date_modify DateModify
date_offset_get DateOffsetGet
date_sub DateSub
date_timestamp_get DateTimestampGet
date_timestamp_set DateTimestampSet
date_timezone_get DateTimezoneGet
date_timezone_set DateTimezoneSet

String Functions

PHP function Golang function
addslashes Addslashes
chr Chr
crc32 Crc32
explode Explode
bin2hex Bin2hex
hex2bin Hex2bin
htmlspecialchars HTMLSpecialchars
htmlspecialchars_decode HTMLSpecialcharsDecode
implode Implode
str_ireplace Ireplace
lcfirst Lcfirst
md5 Md5
md5_file Md5File
sha1 Sha1
sha1_file Sha1File
number_format NumberFormat
ord Ord
str_replace Replace
similar_text SimilarText
stripslashes Stripslashes
mb_stripos Stripos
mb_stristr Stristr
mb_strlen Strlen
mb_strpos Strpos
mb_strripos Strripos
mb_strrpos Strrpos
mb_strstr Strstr
mb_substr Substr
str_pad StrPad
str_repeat StrRepeat
strrev Strrev
strtolower Strtolower
strtoupper Strtoupper
str_word_count StrWordCount
ltrim Ltrim
rtrim Rtrim
trim Trim
ucfirst Ucfirst
ucwords Ucwords

Math Functions

PHP function Golang function
abs Abs
base_convert BaseConvert
bindec Bindec
decbin Decbin
dechex Dechex
hexdec Hexdec
decoct Decoct
octdec Octdec
ceil Ceil
floor Floor
pi Pi
mt_rand MtRand
rand Rand
round Round

Array Functions

PHP function Golang function
array_chunk ArrayChunk
array_column ArrayColumn
array_combine ArrayCombine
array_diff ArrayDiff
array_filter ArrayFilter
array_flip ArrayFlip
array_intersect ArrayIntersect
array_keys ArrayKeys
array_key_exists ArrayKeyExists
array_pad ArrayPad
array_pop ArrayPop
array_push ArrayPush
array_reverse ArraySlice
array_slice ArrayReverse
array_sum ArraySum
array_shift ArrayShift
array_unshift ArrayUnshift
array_unique ArrayUnique
count Count
in_array InArray
key_exists KeyExists
sort Sort
rsort Rsort

Ctype Functions

PHP function Golang function
ctype_alnum CtypeAlnum
ctype_alpha CtypeAlpha
ctype_cntrl CtypeCntrl
ctype_digit CtypeDigit
ctype_graph CtypeGraph
ctype_lower CtypeLower
ctype_print CtypePrint
ctype_punct CtypePunct
ctype_space CtypeSpace
ctype_upper CtypeUpper
ctype_xdigit CtypeXdigit

Filesystem Functions

PHP function Golang function
basename Basename
dirname Dirname DirnameWithLevels
realpath Realpath
touch Touch
unlink Unlink
mkdir Mkdir
rmdir Rmdir
symlink Symlink
link Link
chmod Chmod
chown Chown
is_file IsFile
is_dir IsDir
is_executable IsExecutable
is_readable IsReadable
is_writable IsWritable
is_link IsLink
file_exists FileExists
filesize Filesize
copy Copy
rename Rename
file_get_contents FileGetContents
file_put_contents FilePutContents

Directory Functions

PHP function Golang function
getcwd Getcwd
chdir Chdir
scandir Scandir

Image Functions

PHP function Golang function
getimagesize GetImageSize
getimagesizefromstring GetImageSizeFromString

Network Functions

PHP function Golang function
gethostbyaddr GetHostByAddr
gethostbyname GetHostByName
gethostbynamel GetHostByNamel
gethostname GetHostName
ip2long IP2Long
long2ip Long2IP

JSON Functions

PHP function Golang function
json_decode JSONDecode
json_encode JSONEncode

CSPRNG Functions

PHP function Golang function
random_bytes RandomBytes

URL Functions

PHP function Golang function
base64_decode Base64Decode
base64_encode Base64Encode
get_headers GetHeaders
get_meta_tags GetMetaTags
http_build_query HTTPBuildQuery
parse_url ParseURL
rawurldecode RawURLDecode
rawurlencode RawURLEncode
urldecode URLDecode
urlencode URLEncode

Misc Functions

PHP function Golang function
getenv Getenv
putenv Putenv
memory_get_usage MemoryGetUsage

Install

go get github.com/imartingraham/php

Usage

import (
    "fmt"
    "github.com/imartingraham/php"
)

// Date/Time functions

fmt.Println(php.Strtotime("2017-07-14 02:40:00")) // output: 1500000000

fmt.Println(php.Strtotime("2017-07-14T10:40:00+08:00")) // output: 1500000000

fmt.Println(php.Date("Y-m-d H:i:s", 1500000000)) // output: 2017-07-14 02:40:00

fmt.Println(php.Date("c", 1500000000)) // output: 2017-07-14T02:40:00+00:00

// String functions

str := "abcdef"

fmt.Println(php.Substr(str, 1, 0)) // bcdef
fmt.Println(php.Substr(str, 1, 3)) // bcd
fmt.Println(php.Substr(str, 0, 4)) // abcd
fmt.Println(php.Substr(str, -1, 1)) // f
fmt.Println(php.Substr(str, 0, -1)) // abcde

// Math functions

fmt.Println(php.Round(5.055, 2)) // 5.06

// Array functions

arr := []string{"1", "1", "2", "3", "a", "ab", "abc", "abc", "abc", "Abc"}
fmt.Println(php.ArrayUnique(arr).([]string)) // [abc Abc 1 2 3 a ab]
fmt.Println(php.InArray("a", arr)) // true

For more usage you can find it out from test files.

About

Implements some PHP functions by Golang. This library has no third-party dependency.

License:MIT License


Languages

Language:Go 100.0%