PHP standard library
PHP engine semantics (the value model, juggling and comparison, string, number, JSON, serialize, igbinary, sorting, filter and URL functions) for crates that must behave exactly like PHP.
This library is maintained by the Appwrite team.
Getting started
[]
= { = "utopia-php-std", = "https://github.com/appwrite/appwrite" }
php-std reproduces PHP built-in functions and value semantics in Rust, byte for byte. When you port PHP code, call the php-std twin of each built-in instead of writing your own approximation, and the Rust code gives the same answer as PHP, edge cases included. Strings are bytes (&[u8]), because PHP strings are bytes. This guide covers the common tasks.
Parse a URL like parse_url
parse_url splits a URL into its parts. A missing part is None, and a URL PHP rejects is None as a whole, where PHP returns false.
use parse_url;
let url = parse_url.ok_or?;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
Match a regular expression like preg_match
Patterns take PHP's delimiters and modifiers, and run on PCRE2 10.44 with its JIT, the library PHP bundles, called the way PHP calls it. Compile a pattern once with Regex::compiled and read the groups of a match.
use Regex;
let semver = compiled.map_err?;
let version = semver.captures.ok_or?;
assert_eq!;
assert_eq!;
assert!;
Replace text like preg_replace
preg_replace takes the pattern, replacement and subject as a string or an array, like PHP. The result holds the new value and the number of replacements.
use ;
let replaced = preg_replace?;
assert_eq!;
assert_eq!;
Encode and decode JSON like json_encode
json::encode and json::decode work on Value, PHP's value model. Floats, escaping and flags follow PHP, and a failure returns the error PHP reports.
use ;
use ;
let mut user: Array = new;
user.set;
user.set;
user.set;
let text = encode?;
assert_eq!;
let text = encode?;
assert_eq!;
let decoded = decode?;
let Array = decoded else ;
assert!;
let error = decode.unwrap_err;
assert_eq!;
Compare values like ==
PHP 8's == compares numeric strings as numbers and other strings as text. Value::loose_eq is ==, Value::strict_eq is ===, and Value::cmp_php is <=>.
use Ordering;
use Value;
let hundred: Value = from;
let exponent: Value = from;
let zero: Value = from;
let lower: Value = from;
let ten: Value = from;
let null: Value = Null;
assert!;
assert!;
assert!;
assert!;
assert!;
assert_eq!;
Format numbers like number_format
number_format rounds half away from zero and groups the digits by three. sprintf takes PHP's format string and arguments.
use ;
use Number;
assert_eq!;
assert_eq!;
assert_eq!;
let line = sprintf?;
assert_eq!;
Work with multibyte strings
The mb functions count and cut UTF-8 characters, not bytes. strlen is the byte length of the slice.
use ;
let name = "Zoë Ångström".as_bytes;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Parse a query string like parse_str
parse_str decodes a query string into an array the way PHP fills $_GET. Dots and spaces in names become _, and name[] builds a nested array.
use parse_str;
use Value;
let query = parse_str;
assert_eq!;
let Some = query.get_str else ;
assert_eq!;
Validate input like filter_var
filter_var takes the value, the filter and its flags or options, with PHP's constants. A failed validation returns false, like PHP.
use ;
let email = filter_var?;
assert_eq!;
let ip = filter_var?;
assert_eq!;
let range = Array;
let limit = filter_var?;
assert_eq!;
Contributing
Development happens in appwrite/appwrite, where this crate lives at crates/utopia/php-std. Run these commands from a checkout of it. The contributing guide covers each step.
Test
License
BSD-3-Clause. See LICENSE.