Use Golang to implement PHP's common built-in functions.

PHP2Go

GoDoc Go Report Card MIT licensed

Use Golang to implement PHP's common built-in functions. About 140+ functions have been implemented.

Install

go get github.com/syyongx/php2go

Requirements

Go 1.10 or above.

PHP Functions

Date/Time Functions

time()
strtotime()
date()
checkdate()
sleep()
usleep()

String Functions

strpos()
stripos()
strrpos()
strripos()
str_replace()
ucfirst()
lcfirst()
ucwords()
substr()
strrev()
number_format()
chunk_split()
str_word_count()
wordwrap()
strlen()
mb_strlen()
str_repeat()
strstr()
strtr()
str_shuffle()
trim()
ltrim()
rtrim()
explode()
strtoupper()
strtolower()
chr()
ord()
nl2br()
json_encode()
json_decode()
addslashes()
stripslashes()
quotemeta()
htmlentities()
html_entity_decode()
md5()
md5_file()
sha1()
sha1_file()
crc32()
levenshtein()
similar_text()
soundex()
parse_str()

URL Functions

base64_encode()
base64_decode()
parse_url()
urlencode()
urldecode()
rawurlencode()
rawurldecode()
http_build_query()

Array(Slice/Map) Functions

array_fill()
array_flip()
array_keys()
array_values()
array_merge()
array_chunk()
array_pad()
array_slice()
array_rand()
array_column()
array_push()
array_pop()
array_unshift()
array_shift()
array_key_exists()
array_combine()
array_reverse()
implode()
in_array()

Mathematical Functions

abs()
rand()
round()
floor()
ceil()
pi()
max()
min()
decbin()
bindec()
hex2bin()
bin2hex()
dechex()
hexdec()
decoct()
Octdec()
base_convert()
is_nan()

Directory/Filesystem Functions

stat()
pathinfo()
file_exists()
is_file()
is_dir()
filesize()
file_put_contents()
file_get_contents()
unlink()
delete()
copy()
is_readable()
is_writeable()
rename()
touch()
mkdir()
getcwd()
realpath()
basename()
chmod()
chown()
fclose()
filemtime()
fgetcsv()
glob()

Variable handling Functions

empty()
is_numeric()

Program execution Functions

exec()
system()
passthru()

Network Functions

gethostname()
gethostbyname()
gethostbynamel()
gethostbyaddr()
ip2long()
long2ip()

Misc. Functions

echo()
uniqid()
exit()
die()
getenv()
putenv()
memory_get_usage()
version_compare()
zip_open()
Ternary(condition bool, trueVal, falseVal interface{}) interface{}

LICENSE

PHP2Go source code is licensed under the MIT Licence.

Owner
Comments
  • windows go get fail

    windows go get fail

    $ go get github.com/syyongx/php2go

    github.com/syyongx/php2go

    syyongx\php2go\php_unix.go:11:9: undefined: syscall.Umask syyongx\php2go\php_unix.go:16:8: undefined: syscall.Statfs_t syyongx\php2go\php_unix.go:17:9: undefined: syscall.Statfs syyongx\php2go\php_unix.go:26:8: undefined: syscall.Statfs_t syyongx\php2go\php_unix.go:27:9: undefined: syscall.Statfs

  • about map ,unordered

    about map ,unordered

    Should there be a new structure, the map is defined to be ordered, so that array_value, array_combine and other functions are more accurate, rather than directly using map

  • 将内容切分了一下,修改了几个函数,添加CI

    将内容切分了一下,修改了几个函数,添加CI

    a: 添加travis 测试 f: 修改 Gethostbyaddr 函数 函数中 names[0] 使用前未对 names长度进行判断 m: 修改 IsFile 函数 函数判断文件是否存在的同时还需判断是否不为目录文件 m: 拆分模块 分为不同文件 m: 拆分测试模块,支持windows测试 m: 修改 Pack 和 Unpack 的测试用例 去除输出的内容 改为标准测试

  • trim error

    trim error

    package main
    
    import "github.com/syyongx/php2go"
    import "fmt"
    
    func main() {
            s := "13000"
            fmt.Printf("%s=>%s\n", s, php2go.Trim(s))
    
    }
    
    

    localhost:~$ go run a.go 13000=>13

  • go get fail

    go get fail

    λ go version
    go version go1.7.3 windows/amd64
    λ go get github.com/syyongx/php2go
    # github.com/syyongx/php2go
    D:\golangcode\src\github.com\syyongx\php2go\php.go:977: u.Hostname undefined (type *url.URL has no field or method Hostname)
    D:\golangcode\src\github.com\syyongx\php2go\php.go:980: u.Port undefined (type *url.URL has no field or method Port)
    D:\golangcode\src\github.com\syyongx\php2go\php.go:1589: undefined: syscall.Statfs_t
    D:\golangcode\src\github.com\syyongx\php2go\php.go:1590: undefined: syscall.Statfs
    D:\golangcode\src\github.com\syyongx\php2go\php.go:1600: undefined: syscall.Statfs_t
    D:\golangcode\src\github.com\syyongx\php2go\php.go:1601: undefined: syscall.Statfs
    D:\golangcode\src\github.com\syyongx\php2go\php.go:1615: undefined: syscall.Umask
    
  • Empty优化

    Empty优化

    // Empty empty() func Empty(val interface{}) bool { //if val == nil { // return true //} //v := reflect.ValueOf(val) //switch v.Kind() { //case reflect.String, reflect.Array: // return v.Len() == 0 //case reflect.Map, reflect.Slice: // return v.Len() == 0 || v.IsNil() //case reflect.Bool: // return !v.Bool() //case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: // return v.Int() == 0 //case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: // return v.Uint() == 0 //case reflect.Float32, reflect.Float64: // return v.Float() == 0 //case reflect.Interface, reflect.Ptr: // return v.IsNil() //} // //return reflect.DeepEqual(val, reflect.Zero(v.Type()).Interface())

       if val == nil {
            return true
    }
    v := reflect.ValueOf(val)
    switch v.Kind() {
    	case reflect.Map, reflect.Slice,reflect.Chan:
    		return v.Len() == 0 || v.IsNil()
    	default:
    		return v.IsZero()
    }
    

    }

  • Invalid syntax while trying to call Bin2hex

    Invalid syntax while trying to call Bin2hex

    The error "strconv.ParseInt: parsing "你好世界": invalid syntax" is returned while trying to run the test code below.

    package main
    
    import (
    	"fmt"
    	"strconv"
    	php "github.com/syyongx/php2go"
    )
    
    func Bin2hex(str string) (string, error) {
    	byteArray := []byte(str)
    	var out string;
    	for i:=0; i < len(byteArray); i++{
    		out += strconv.FormatInt(int64(byteArray[i]), 16)
    	}
    	return out,nil
    }
    
    
    
    func main() {
    	fmt.Println(Bin2hex("你好世界"))
    	fmt.Println(php.Bin2hex("你好世界"))
    }
    
    

    Results as follow.

    C:\Users\Toby\Desktop\golang\fsadapt>fsadapt.exe
    e4bda0e5a5bde4b896e7958c <nil>
     strconv.ParseInt: parsing "你好世界": invalid syntax
    

    System Information: OS: Windows 7 64 bit Go version: go version go1.13.4 windows/amd64

  • 32位的机器  随机数方法会溢出

    32位的机器 随机数方法会溢出

    func Rand(min, max int) int { if min > max { panic("min: min cannot be greater than max") } r := rand.New(rand.NewSource(time.Now().UnixNano())) n := r.Intn(math.MaxInt32) return n/((math.MaxInt32+1)/(max-min+1)) + min return 1 }

    把加一去掉就可以了

  • Hex2Bin  error

    Hex2Bin error

    package main

    import ( "fmt" "strconv" )

    // Hex2bin hex2bin() func Hex2bin(data string) (string, error) { i, err := strconv.ParseInt(data, 16, 0) if err != nil { return "", err } return strconv.FormatInt(i, 2), nil }

    func main(){ fmt.Println(Hex2bin("48656c6c6f20576f726c6421")) }

    When I used this method, it returned an error below. //strconv.ParseInt: parsing "48656c6c6f20576f726c6421": value out of range

    If I use php function hex2bin()

    :

    It shows us ,

    Hello World!

Related tags
Helper functions for common scenarios, using Go generics.

zeroflucs generics When writing Go code for Go 1.17 or below, we've all written more than our fair share of methods to check "does this slice contain

Feb 18, 2022
golang implement gossip algorithm
golang implement gossip algorithm

golang implement gossip algorithm

Dec 1, 2022
Golang code-generators used to implement Kubernetes-style API types.

code-generator Golang code-generators used to implement Kubernetes-style API types. Purpose These code-generators can be used in the context of Custom

Dec 30, 2022
Experimenting with golang generics to implement functional favorites like filter, map, && reduce.

funcy Experimenting with golang generics to implement functional favorites like filter, map, && reduce. 2021-12 To run the tests, you need to install

Dec 29, 2021
common tools for golang

utils common tools for golang package main

Dec 27, 2021
cuckoo-filter go implement. config by you 布谷鸟过滤器的Go实现,可以定制化过滤器参数

Cuckoo filter is a Bloom filter replacement for approximated set-membership queries. While Bloom filters are well-known space-efficient data structures to serve queries like "if item x is in a set?", they do not support deletion. Their variances to enable deletion (like counting Bloom filters) usually require much more space.

Jan 1, 2023
Go implement D*Lite with SDL2
Go implement D*Lite with SDL2

dstarlite-gosdl Go implement D*Lite with SDL2 使用Go SDL2库实现的D*Lite算法 注意,尽可能使用 go mod ,自动下载依赖库(确保已经安装了SDL2库) 推荐使用Linux或者macOS运行 git clone https://github

Apr 2, 2022
An implement of CNI depending on neutron API

An implement of CNI depending on neutron API

Dec 27, 2021
sigurls is a reconnaissance tool, it fetches URLs from AlienVault's OTX, Common Crawl, URLScan, Github and the Wayback Machine.

sigurls is a reconnaissance tool, it fetches URLs from AlienVault's OTX, Common Crawl, URLScan, Github and the Wayback Machine. DiSCLAIMER: fe

May 22, 2021
a decision & trigger framework backed by Google's Common Expression Language used in graphikDB

a decision & trigger framework backed by Google's Common Expression Language used in graphikDB

Nov 15, 2022
redis-util business-friendly encapsulation of redis operations, such as the common cache set get operation

redis-util 方便业务使用的redis操作封装,比如常见的缓存set get操作, 一行代码搞定,不像开源库需要写好多行 使用方法

Oct 22, 2021
Common utils that need in every development

Utils modules Common utils that need in every development Modules Conversion String IntToString(num int) string Int8ToString(num int8) string Int16ToS

Nov 5, 2021
With Pasteback you can store common terms and paste them back to clipboard

Pasteback With Pasteback you can store common terms and paste them back to clipboard when needed. There is one central spot to put every string into y

Nov 22, 2021
Higher Order Functions using Golang Generics (Hack Days 2022)

hoff: Higher Order Functions (and Friends) Golang 1.18+ implementations of common methods/data structures using Go Generics Requirements Go 1.18 or ne

Jan 4, 2023
A super simple Lodash like utility library with essential functions that empowers the development in Go
A super simple Lodash like utility library with essential functions that empowers the development in Go

A simple Utility library for Go Go does not provide many essential built in functions when it comes to the data structure such as slice and map. This

Jan 4, 2023
This is an open source project for commonly used functions for the Go programming language.

Common Functions This is an open source project for commonly used functions for the Go programming language. This package need >= go 1.3 Code Conventi

Jan 8, 2023
randstr is a module that contains functions for generating random strings.

randstr is a module that contains functions for generating random strings. The functions in this module uses the crypto/rand package. Installa

Nov 13, 2021
Go linter that warns about number of arguments in functions.

argslen linter Argslen is a linter that checks for long list of argument in functions. The default limit is 5 (maxArguments) and skip the test files (

Sep 17, 2022
Utility functions for work with the Kubernetes Go-Client

go-k8s-utils This repository contains utils for the work with Kubernetes, in specific with the go-client library. Testing This package contains utils

Dec 14, 2022