GithubHelp home page GithubHelp logo

php2go's Introduction

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()
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()

Multibyte String Functions

mb_strlen()
mb_strtoupper()

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()

CSPRNG Functions

random_bytes()
random_int()

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()
memory_get_peak_usage()
version_compare()
zip_open()
Ternary(condition bool, trueVal, falseVal interface{}) interface{}

LICENSE

PHP2Go source code is licensed under the MIT Licence.

php2go's People

Contributors

dreamans avatar filego avatar l3dlp avatar moonjihae avatar phper08 avatar syyongx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php2go's Issues

round function

round function in php have tow params,but in this pkg just only one param;It is can not return a decimal.

ParseStr

I have a library parsestr that emulates PHP's parse_str behaviour.

I just thought you may be interested to give it a look as you note that yours does not quite do the deed.

base64_decode遇到一个问题

编码后的字符串如果不带==,用go解密后的结果与php解密结果会有差异,建议判断一下如果没有==自动补齐后再解密就与php的一至了

TestTime fails

TestTime test fails with go 1.14.2 in UTC+1 timezone.

$ go version
go version go1.14.2 linux/amd64
$ go test -v
=== RUN   TestTime
    TestTime: php_test.go:312: Expected 27/04/2018 11:23:14 AM (type string) - Got 27/04/2018 04:23:14 AM (type string)
--- FAIL: TestTime (0.00s)
$ 

The reason is likely that time.Unix() uses local time input, which means that test will faily every time it is run outside of author's timezone.

Test:

equal(t, "27/04/2018 11:23:14 AM", Date("02/01/2006 15:04:05 PM", 1524799394))

Code:

func Date(format string, timestamp int64) string {
	return time.Unix(timestamp, 0).Format(format)
}

This should ideally be fixed so tests can be run successfully in any timezone.

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

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

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()
}

}

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

Bug in Strstr function when haystack equals needle

What happened?

The Strstr function is expected to return the entire haystack string when the haystack and needle parameters are identical. However, in the current implementation, it returns only the last character of the haystack string.

Expected behavior

fmt.Println(Strstr("hello world", "hello world")) // hello world

Actual results

fmt.Println(Strstr("hello world", "hello world")) // d

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

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

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!

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
}

把加一去掉就可以了

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.