Categorygithub.com/imartingraham/php
repositorypackage
0.6.3
Repository: https://github.com/imartingraham/php.git
Documentation: pkg.go.dev

# README

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:

OSGo version
Linux1.9
OSX1.12
Windows1.13

Date/Time Functions

PHP functionGolang function
checkdateCheckdate
dateDate
strtotimeStrtotime
timeTime
microtimeMicrotime
date_addDateAdd
date_create_from_formatDateCreateFromFormat
date_createDateCreate
date_date_setDateDateSet
date_default_timezone_getDateDefaultTimezoneGet
date_default_timezone_setDateDefaultTimezoneSet
date_diffDateDiff
date_formatDateFormat
date_interval_create_from_date_stringDateIntervalCreateFromDateString
date_isodate_setDateISODateSet
date_modifyDateModify
date_offset_getDateOffsetGet
date_subDateSub
date_timestamp_getDateTimestampGet
date_timestamp_setDateTimestampSet
date_timezone_getDateTimezoneGet
date_timezone_setDateTimezoneSet

String Functions

PHP functionGolang function
addslashesAddslashes
chrChr
crc32Crc32
explodeExplode
bin2hexBin2hex
hex2binHex2bin
htmlspecialcharsHTMLSpecialchars
htmlspecialchars_decodeHTMLSpecialcharsDecode
implodeImplode
str_ireplaceIreplace
lcfirstLcfirst
md5Md5
md5_fileMd5File
sha1Sha1
sha1_fileSha1File
number_formatNumberFormat
ordOrd
str_replaceReplace
similar_textSimilarText
stripslashesStripslashes
mb_striposStripos
mb_stristrStristr
mb_strlenStrlen
mb_strposStrpos
mb_strriposStrripos
mb_strrposStrrpos
mb_strstrStrstr
mb_substrSubstr
str_padStrPad
str_repeatStrRepeat
strrevStrrev
strtolowerStrtolower
strtoupperStrtoupper
str_word_countStrWordCount
ltrimLtrim
rtrimRtrim
trimTrim
ucfirstUcfirst
ucwordsUcwords

Math Functions

PHP functionGolang function
absAbs
base_convertBaseConvert
bindecBindec
decbinDecbin
dechexDechex
hexdecHexdec
decoctDecoct
octdecOctdec
ceilCeil
floorFloor
piPi
mt_randMtRand
randRand
roundRound

Array Functions

PHP functionGolang function
array_chunkArrayChunk
array_columnArrayColumn
array_combineArrayCombine
array_diffArrayDiff
array_filterArrayFilter
array_flipArrayFlip
array_intersectArrayIntersect
array_keysArrayKeys
array_key_existsArrayKeyExists
array_padArrayPad
array_popArrayPop
array_pushArrayPush
array_reverseArraySlice
array_sliceArrayReverse
array_sumArraySum
array_shiftArrayShift
array_unshiftArrayUnshift
array_uniqueArrayUnique
countCount
in_arrayInArray
key_existsKeyExists
sortSort
rsortRsort

Ctype Functions

PHP functionGolang function
ctype_alnumCtypeAlnum
ctype_alphaCtypeAlpha
ctype_cntrlCtypeCntrl
ctype_digitCtypeDigit
ctype_graphCtypeGraph
ctype_lowerCtypeLower
ctype_printCtypePrint
ctype_punctCtypePunct
ctype_spaceCtypeSpace
ctype_upperCtypeUpper
ctype_xdigitCtypeXdigit

Filesystem Functions

PHP functionGolang function
basenameBasename
dirnameDirname DirnameWithLevels
realpathRealpath
touchTouch
unlinkUnlink
mkdirMkdir
rmdirRmdir
symlinkSymlink
linkLink
chmodChmod
chownChown
is_fileIsFile
is_dirIsDir
is_executableIsExecutable
is_readableIsReadable
is_writableIsWritable
is_linkIsLink
file_existsFileExists
filesizeFilesize
copyCopy
renameRename
file_get_contentsFileGetContents
file_put_contentsFilePutContents

Directory Functions

PHP functionGolang function
getcwdGetcwd
chdirChdir
scandirScandir

Image Functions

PHP functionGolang function
getimagesizeGetImageSize
getimagesizefromstringGetImageSizeFromString

Network Functions

PHP functionGolang function
gethostbyaddrGetHostByAddr
gethostbynameGetHostByName
gethostbynamelGetHostByNamel
gethostnameGetHostName
ip2longIP2Long
long2ipLong2IP

JSON Functions

PHP functionGolang function
json_decodeJSONDecode
json_encodeJSONEncode

CSPRNG Functions

PHP functionGolang function
random_bytesRandomBytes

URL Functions

PHP functionGolang function
base64_decodeBase64Decode
base64_encodeBase64Encode
get_headersGetHeaders
get_meta_tagsGetMetaTags
http_build_queryHTTPBuildQuery
parse_urlParseURL
rawurldecodeRawURLDecode
rawurlencodeRawURLEncode
urldecodeURLDecode
urlencodeURLEncode

Misc Functions

PHP functionGolang function
getenvGetenv
putenvPutenv
memory_get_usageMemoryGetUsage

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.