Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129767 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id B0AEA1A00BC for ; Fri, 16 Jan 2026 19:16:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1768591022; bh=fEt/D6oECe4/NNFCGUcbr6/OfbDYBTtD6z+O88p/EjI=; h=From:Date:Subject:To:From; b=nhUonS5eCFx+BuvZ0qwjgKs/lGpimTLsxUxd3AepP6Xueu5W+/YjZz58oEZbVQnNS 2jblnclaZSe8hW7ziKZBOZ+yHSXElTgfEn/jtXybom0X3Ecz4y5bCaRr7Q2sopegUt rnjc4j2Stx9QbJ7k0DFyv30oGdP1h4Vjfy4rQeGxoupXKWxGb8w4mzEDuI9rRjM+ez NR5jp/AqXbGRHyslDtTFOvqTjWUAUU3ne3reUT7kHF4sMEvCg+B0kgsMYPka/8iD9u E/NFWtvsTN7gx/NnILDECrlpGgpOZPUVPidDMBDm4mGg4Tr50HpVUq7H2KpxriimKF /NzoSY4vJh/Mg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7A1431801E1 for ; Fri, 16 Jan 2026 19:17:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 16 Jan 2026 19:16:58 +0000 (UTC) Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-b8710c9cddbso320983666b.2 for ; Fri, 16 Jan 2026 11:16:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768591012; x=1769195812; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=fEt/D6oECe4/NNFCGUcbr6/OfbDYBTtD6z+O88p/EjI=; b=HwJG6us/BfSmiEPZL2DpAN1HqKCx71plYt5/pO/5KQG0o0Nv3bVGizWRk+jMp8ur4Z Y5z5bT/nAFwNGqdwi8TGgzd05Y1SeeneV5jY7c2X9DL2fuU4K75cjjkeiXf+T0yS5Qk7 6ypCBxPFOIzoynZJ/OrSCK8XScd6g3dJvKasHbYVMgOQcIgB/CEXTcMpyPB8ZC4aBZVq 7gQ/JqUjoan2z3Fo6tedcguSElBpsz2v2Qm+UnmDvp43NBhiCeBwNKafrrGylpylGFsg k5WbCIyOBRjYpPKzjJEphl8chms+5VZw0FxytVQ5xCsNbxjfl+N/9BfdWqJmtCBlOJ4W fSKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768591012; x=1769195812; h=to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fEt/D6oECe4/NNFCGUcbr6/OfbDYBTtD6z+O88p/EjI=; b=SKB4thed6Fc3WdAcOvZpQWcNqZDNZIXLgp4mEaXzqv2Ys8zmD8tYDGjVqsTVKbz3VV UpyqIgtDYMaP4U0SmfGk3hdSEnqPE3wVSEDHr77yQD508Zi4YlWwGu4e4yfakxy9L1pz wr48uo6+qSltO7WOcnjClSrCikZce9NqGBqho0xzGXbz+Xzx8tnnqBJEZzdDnNF57DAP 0R1p03CxMV/X2n5dl44RXFgObCPe2EBzJXfOhjIrTxgCo16W+OfUoJobSTYNfBvcAN5k yy4SDlYC0sPqEm6ldiL3Xg8vqCqDohjlg6LRMxFd03BsOVd9dwLYYHF1fQFXDZhTsC5t ZsGA== X-Gm-Message-State: AOJu0YxTd26+sjsBVnKXKbJacJ2yFA9rpLlBd+c2UVlsvPnFL/PHQAG2 5sCOvkPWGES35PhBQcPpxcoUFDfU+KeuEPa5gSkonKsY95Hr0ZDNkb5amviZ9xr1kKdcxxPXGqd 7jUaSVLr74fQbcrwRnChoZ86rTT2cexTIEAYq X-Gm-Gg: AY/fxX5hNMUrIHU4Y4bt/LbguLlyZf8jftLLebzkqHTPV/k4VwMpxGMmJF8zY0xMChR VbhMCa6VlnM7yOgSHf/qihf8BTu6a/AIjWAjUenRzT32TgqaHW5EqxwuirMNMeUbiI1dI5px6OO FxuKCnuWncMxAjaBVAqfMYHUEGz+1QCDj3dfTJCOfczpgdi2IKAgKYQckkCg9Srk/2n2y43wItt MJDc+5bvNv5CM+Y+7fbKRTOGRVexPBvXVJIckCXUfLutQbpKLx7ydlrZo4ZylVT9fZbx1c+EuOA 8ss= X-Received: by 2002:a17:907:868f:b0:b76:8164:88b5 with SMTP id a640c23a62f3a-b8796b40ddbmr275078266b.46.1768591011338; Fri, 16 Jan 2026 11:16:51 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Fri, 16 Jan 2026 20:16:39 +0100 X-Gm-Features: AZwV_QjAyUbXm4NG8it8aZiD8AtCzyKfTcCuf47m-M3_7qni_R4a0YXMD9j8hFM Message-ID: Subject: [PHP-DEV] Proposal for functions that handle prefixes and suffixes To: PHP internals Content-Type: multipart/alternative; boundary="000000000000fbdc0f0648862e25" From: barel.barelon@gmail.com (Barel) --000000000000fbdc0f0648862e25 Content-Type: text/plain; charset="UTF-8" Hi I have just created a PR with the implementation of six new functions which can be used to manage suffixes and prefixes in strings. I did not actually plan to create the PR just yet, I just wanted to add the implementation as a sample of what I wanted to achieve and wanted to create the PR in my own fork but somehow I managed to actually create it in the main repo. This is the PR https://github.com/php/php-src/pull/20953 Nevertheless, the idea is that in php code many times we have to deal with prefixes or suffixes of strings, this comes up a lot when you are dealing with file names or urls. Some examples - If the domain includes a initial www part, remove it - If the filename includes the .png extension, remove it - If the URL does not include the http:// schema, add it - If the URL is missing a trailing slash, add it - If the URL starts with http://, change it to https:// - If the filename ends in .jpeg, change it to .jpg - etc... It is not too difficult to implement this functionality by using the str_starts_with, str_ends_with and the substr functions or with preg_replace but I think it would be good to have some functions that can perform these operations in a single step, creating code which is simpler to write and to understand My proposal adds six functions: function add_prefix(string $source, string $prefix): string function add_suffix(string $source, string $suffix): string Add a prefix or suffix to a string *only if the string does not yet have it* function remove_prefix(string $source, string $prefix): string function remove_suffix(string $source, string $suffix): string Remove a prefix or suffix from a string if the string has it function replace_prefix(string $source, string $prefix, string $replace): string function replace_suffix(string $source, string $suffix, string $replace): string Replace a prefix or suffix in a string it the string has it I would like to know if the internals people think that the addition of these functions would be interesting. If there is some interest I will prepare a full RFC This is my first contribution to php-src so I would be grateful for any advice or hints :-) Cheers Carlos --000000000000fbdc0f0648862e25 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

I have just created a PR with the implementation= of six new functions which can be used to manage suffixes and prefixes in = strings. I did not actually plan to create the PR just yet, I just wanted t= o add the implementation as a sample of what I wanted to achieve and wanted= to create the PR in my own fork but somehow I managed to actually create i= t in the main repo.

This is the PR=C2=A0https://github.com/php/php-src/pull/20953
=
Nevertheless, the idea is that in php code many times we have to deal w= ith prefixes or suffixes of strings, this comes up a lot when you are deali= ng with file names or urls. Some examples

- If the domain includes a= initial www part, remove it
- If the filename includes the .png extensi= on, remove it
- If the URL does not include the http:// schema, add it- If the URL is missing a trailing slash, add it
- If the URL starts w= ith http://, change it to https://
- If the filename ends in .jpeg, chan= ge it to .jpg
- etc...

It is not too difficult to implement this = functionality by using the str_starts_with, str_ends_with and the substr fu= nctions or with preg_replace but I think it would be good to have some func= tions that can perform these operations in a single step, creating code whi= ch is simpler to write and to understand

My proposal adds six functi= ons:

function add_prefix(string $source, string $prefix): stringfunction add_suffix(string $source, string $suffix): string

=
Add a prefix or suffix to a string *only if the string does not = yet have it*

function remove_prefix(string $source, string $prefix):= string
function remove_suffix(string $source, string $suffix): string
Remove a prefix or suffix from a string if the string has it

f= unction replace_prefix(string $source, string $prefix, string $replace): st= ring
function replace_suffix(string $source, string $suffix, string $rep= lace): string

Replace a prefix or suffix in a string it the string h= as it

I would like to know if the internals people think that the ad= dition of these functions would be interesting. If there is some interest I= will prepare a full RFC

This is my first contribution to php-src so= I would be grateful for any advice or hints :-)

Cheers

Carlo= s

--000000000000fbdc0f0648862e25--