From aef0533d9b1aec077255f78a037e873d2e2e5e6a Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 30 Apr 2025 13:59:18 +0400 Subject: [PATCH] ajout str::split --- php/src/str.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/php/src/str.php b/php/src/str.php index 347726b..2dc71ba 100644 --- a/php/src/str.php +++ b/php/src/str.php @@ -328,6 +328,22 @@ class str { else return preg_split('/\s+/', $s); } + /** + * découper la chaine: + * - avec preg_split si $sep est une expression régulière /re/ + * - avec explode sinon + */ + static final function split(string $sep, ?string $s): ?array { + if ($s === null) return null; + if ($sep === "") { + return [$s]; + } elseif (substr($sep, 0, 1) === "/") { + return preg_split($sep, $s); + } else { + return explode($sep, $s); + } + } + /** * joindre les éléments de $parts comme avec implode(), mais en ignorant les * valeurs fausses (cela n'inclue pas la chaine "0")