PHP standard library
Rust 37,994 lines (33,127 code)split
| Part | Code | Comments | Blank | Total | Files |
|---|---|---|---|---|---|
| Rust source | 33,127 | 3,282 | 1,585 | 37,994 | 48 |
| Rust tests | 1,549 | 29 | 118 | 1,696 | 32 |
Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.
PHP functions
Browse all 146 →The PHP engine's own behaviour, reimplemented in Rust so the Utopia crates match PHP byte for byte, each function checked against the real one by recorded cases and fuzzing on every run.
| Part | Code | Comments | Blank | Total | Files |
|---|---|---|---|---|---|
| Rust source | 33,127 | 3,282 | 1,585 | 37,994 | 48 |
| Rust tests | 1,549 | 29 | 118 | 1,696 | 32 |
Install#
The crate lives in the Appwrite repository; depend on it from Git.
[dependencies]
php-std = { git = "https://github.com/appwrite/appwrite" }Getting started#
Common tasks with PHP standard library, each a complete program you can copy and run.
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 php_std::url::parse_url;
let url = parse_url(b"https://user:secret@cloud.appwrite.io:8443/v1/users?limit=25#top").ok_or("invalid URL")?;
assert_eq!(url.scheme().as_deref(), Some(&b"https"[..]));
assert_eq!(url.host().as_deref(), Some(&b"cloud.appwrite.io"[..]));
assert_eq!(url.port(), Some(8443));
assert_eq!(url.path().as_deref(), Some(&b"/v1/users"[..]));
assert_eq!(url.query().as_deref(), Some(&b"limit=25"[..]));
assert!(parse_url(b"http://:80").is_none());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 php_std::pcre::Regex;
let semver = Regex::compiled(b"/^(\\d+)\\.(\\d+)\\.(\\d+)$/").map_err(|_| "invalid pattern")?;
let version = semver.captures(b"1.6.2").ok_or("no match")?;
assert_eq!(version.get(0), Some(&b"1.6.2"[..]));
assert_eq!(version.get(2), Some(&b"6"[..]));
assert!(!semver.is_match(b"1.6"));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 php_std::pcre::{StrOrArray, Value, preg_replace};
let replaced = preg_replace(
&StrOrArray::Str(b"/[^a-z0-9]+/i"),
&StrOrArray::Str(b"-"),
&StrOrArray::Str(b"Hello, World 2026!"),
-1,
)?;
assert_eq!(replaced.value.result, Value::Str(b"Hello-World-2026-".to_vec()));
assert_eq!(replaced.value.count, 3);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 php_std::json::{self, DEFAULT_DEPTH, Flags};
use php_std::{Array, Value};
let mut user: Array = Array::new();
user.set("name".into(), Value::from("Ana"));
user.set("score".into(), Value::from(4.5));
user.set("site".into(), Value::from("https://appwrite.io"));
let text = json::encode(&Value::Array(user.clone()), Flags::NONE, DEFAULT_DEPTH)?;
assert_eq!(text, r#"{"name":"Ana","score":4.5,"site":"https:\/\/appwrite.io"}"#);
let text = json::encode(&Value::Array(user), Flags::UNESCAPED_SLASHES, DEFAULT_DEPTH)?;
assert_eq!(text, r#"{"name":"Ana","score":4.5,"site":"https://appwrite.io"}"#);
let decoded = json::decode(br#"{"tags":["a","b"]}"#, Some(true), DEFAULT_DEPTH, Flags::NONE)?;
let Value::Array(decoded) = decoded else { panic!("expected an array") };
assert!(matches!(decoded.get_str("tags"), Some(Value::Array(tags)) if tags.len() == 2));
let error = json::decode(b"{bad", None, DEFAULT_DEPTH, Flags::NONE).unwrap_err();
assert_eq!(error.message(), "Syntax error");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 std::cmp::Ordering;
use php_std::Value;
let hundred: Value = Value::from(100);
let exponent: Value = Value::from("1e2");
let zero: Value = Value::from(0);
let lower: Value = Value::from("abc");
let ten: Value = Value::from("10");
let null: Value = Value::Null;
assert!(hundred.loose_eq(&exponent));
assert!(!hundred.strict_eq(&exponent));
assert!(!zero.loose_eq(&Value::from("a")));
assert!(!lower.loose_eq(&Value::from("ABC")));
assert!(null.loose_eq(&Value::from(false)));
assert_eq!(ten.cmp_php(&Value::from("9")), Ordering::Greater);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 php_std::format::{Arg, number_format, sprintf};
use php_std::value::Number;
assert_eq!(number_format(Number::Float(1234567.891), 2, b".", b","), b"1,234,567.89");
assert_eq!(number_format(Number::Float(1234.5), 0, b".", b","), b"1,235");
assert_eq!(number_format(Number::Int(1234567), 2, b",", b" "), b"1 234 567,00");
let line = sprintf(b"%-8s|%05.1f|%x", &[Arg::Str(b"cpu"), Arg::Float(3.14159), Arg::Int(255)])?;
assert_eq!(line, b"cpu |003.1|ff");Work with multibyte strings
The mb functions count and cut UTF-8 characters, not bytes. strlen is the byte length of the slice.
use php_std::mb::{mb_strlen, mb_strtolower, mb_substr};
let name = "Zoë Ångström".as_bytes();
assert_eq!(name.len(), 15);
assert_eq!(mb_strlen(name), 12);
assert_eq!(mb_substr(name, 0, Some(3))?, "Zoë".as_bytes());
assert_eq!(mb_strtolower(name), "zoë ångström".as_bytes());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 php_std::encoding::parse_str;
use php_std::Value;
let query = parse_str(b"user.name=Ana+Lee&tags[]=a&tags[]=b");
assert_eq!(query.get_str("user_name"), Some(&Value::from("Ana Lee")));
let Some(Value::Array(tags)) = query.get_str("tags") else { panic!("expected an array") };
assert_eq!(tags.get(&1.into()), Some(&Value::from("b")));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 php_std::filter::{
FILTER_FLAG_IPV4, FILTER_VALIDATE_EMAIL, FILTER_VALIDATE_INT, FILTER_VALIDATE_IP, Key, Options, Value, filter_var,
};
let email = filter_var(&Value::Str(b"ana@appwrite.io".to_vec()), FILTER_VALIDATE_EMAIL, &Options::Flags(0))?;
assert_eq!(email.value, Value::Str(b"ana@appwrite.io".to_vec()));
let ip = filter_var(&Value::Str(b"::1".to_vec()), FILTER_VALIDATE_IP, &Options::Flags(FILTER_FLAG_IPV4))?;
assert_eq!(ip.value, Value::Bool(false));
let range = Options::Array(vec![(
Key::Str(b"options".to_vec()),
Value::Array(vec![(Key::Str(b"min_range".to_vec()), Value::Int(1)), (Key::Str(b"max_range".to_vec()), Value::Int(100))]),
)]);
let limit = filter_var(&Value::Str(b" 25 ".to_vec()), FILTER_VALIDATE_INT, &range)?;
assert_eq!(limit.value, Value::Int(25));Reference#
PHP engine semantics in Rust.
Utopia crates must behave exactly like their PHP libraries, and much of
that behaviour comes from the PHP engine rather than the library: type
juggling, is_numeric, (string) casts, loose comparison, strtolower,
mb_*, number_format, json_encode/json_decode, filter_var,
parse_url. Each is reimplemented here once, and verified against the
real PHP function by bin/compat (library php-std, see
tests/compat/php-std). Utopia crates use these functions instead of
their own approximations (crates/CONVERSION.md §3).
PHP values are Value (types): byte strings, ordered arrays with
integer and string keys, stdClass, and crate-defined objects. The
codecs (json, serialize, igbinary), comparisons and
sort work on it. Older string-level helpers (value, string,
format, filter) take request-model serde_json::Values, decoded
the way Utopia\Http\Request decodes bodies (objects are associative
arrays, except empty objects, which stay stdClass; Value::from_json
is the same mapping).
Modules#
Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.
API#
What php_std exports at its root.