Skip to content

Bring PHP on your browser with the client-side Javascript library PHPjed

License

Notifications You must be signed in to change notification settings

misaki-web/phpjed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About PHPjed

PHPjed

PHPjed (PHP javascripted) brings PHP on your browser. It's a collection of about 275 PHP functions ported to Javascript and ready to be used on a website as a small client-side library (27kB minified and gzipped). A few examples of PHP functions ported are array_multisort, date, htmlentities, isset, preg_replace, setcookie, str_replace, trim, etc.

Installation

Include the minified file on your website. There are two PHPjed distributions:

  1. The light distribution has 273 functions (92kB minified, 27kB gzipped*). The following HTML tag includes the latest version:

     <script src="https://cdn.jsdelivr.net/gh/misaki-web/phpjed/dist/phpjed.min.js"></script>
    

    It's possible to include a specific version, for example 1.0:

     <script src="https://cdn.jsdelivr.net/gh/misaki-web/[email protected]/dist/phpjed.min.js"></script>
    
  2. The complete distribution has 287 functions (142kB minified, 43kB gzipped*). The following HTML tag includes the latest version:

     <script src="https://cdn.jsdelivr.net/gh/misaki-web/phpjed/dist/phpjed-all.min.js"></script>
    

    It's possible to include a specific version, for example 1.0:

     <script src="https://cdn.jsdelivr.net/gh/misaki-web/[email protected]/dist/phpjed-all.min.js"></script>
    

    This distribution contains the functions from the light distribution and also includes the following functions:

    • addcslashes
    • crc32
    • date_parse
    • get_html_translation_table
    • metaphone
    • pack
    • sscanf
    • strptime
    • strtotime
    • unserialize
    • var_dump
    • var_export
    • xdiff_string_diff
    • xdiff_string_patch

All versions available are in the folder dist. The latest version number can be retrieved programmatically from the URL https://raw.githubusercontent.com/misaki-web/phpjed/main/version.txt. Note that unminified and beautified versions of PHPjed are present in the folder dist/unminified.

(*): When servers are configured to send data compressed with the deflate algorithm, this is the actual size of the file downloaded by users.

Usage

After including PHPjed on your website, functions are invoked using the namespace phpjed. Examples:

var subject = "Hello world.";
var search = ["Hello", "world"];
var replace = ["Hi", "everybody"];
var hi_everybody = phpjed.str_replace(search, replace, subject);

PHP functions ported to Javascript

Each function listed below is identified as follows:

  • (LIGHT): included in the light (default) distribution of PHPjed
  • (ALL): included in the complete distribution that contains all functions

A link to the corresponding function on the PHP manual is added.

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

_

  • (LIGHT) _php_cast_float: helper function
  • (LIGHT) _php_cast_int: helper function
  • (LIGHT) _php_cast_string: helper function

Technicals

PHPjed is based on Locutus (formerly php.js). Locutus is packaged for Node.js and distributed through npm. It requires a few Node.js modules and is not suitable to use as a client-side library.

PHPjed has a custom builder (phpjed-builder.sh, developed and tested on GNU/Linux and Bash 5) to achieve the following points:

  • merge all required files from the Locutus npm package and get all code in a single file
  • convert the "module exports" and "require" instructions to have a file usable in browsers
  • remove functions dependant on Node.js modules (these functions are server-side only)
  • remove deprecated and undocumented PHP functions as of PHP 8
  • for improved security, remove functions that make use of eval()
  • rename camel case functions
  • put larger functions in a separate distribution
  • add a namespace (phpjed by default) to prevent naming collisions
  • minify the generated Javascript files

Help about the PHPjed builder can be invoked with ./phpjed-builder.sh -h. PHPjed is distributed with prebuilt Javascript files in the folder dist, but one can build PHPjed themselves, for example to have a custom namespace.

After running the builder, generated files are saved in a newly created subfolder in data/custom-builds. Here's an example for the light (default) distribution:

  • Folder: /tmp/phpjed/data/custom-builds/20210901-143916-53
  • Namespace: phpjed; example: phpjed.is_int(5)
  • Distribution: light (default)
FILE NAME SIZE (in KiB) SIZE REDUCTION
phpjed.js 361.31 -
phpjed.min.js 92.35 74.44%
phpjed.min.js.gz 26.90 92.56%

Here's an example for the complete distribution including all functions:

  • Folder: /tmp/phpjed/data/custom-builds/20210901-144109-39
  • Namespace: phpjed; example: phpjed.is_int(5)
  • Distribution: all
FILE NAME SIZE (in KiB) SIZE REDUCTION
phpjed-all.js 487.94 -
phpjed-all.min.js 142.27 70.84%
phpjed-all.min.js.gz 43.26 91.14%

License

PHPjed (the builder and Javascript files generated) is released under the MIT License. See the file LICENSE.md.

About

Bring PHP on your browser with the client-side Javascript library PHPjed

Topics

Resources

License

Stars

Watchers

Forks