Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129775 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 BB5AE1A00BC for ; Sun, 18 Jan 2026 11:29:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1768735762; bh=f7UuBjRzk1p4MySkPTqZCvV9zsRq8Zow7+4KedHsq7w=; h=Date:Subject:To:References:From:In-Reply-To:From; b=AtWjI6RJRAW+TVF6m4Aa6Om7dieb5/UL+j0pAhsFtWodgy2jHi+hggGLZeePbIMPl FXF5Pg4rfKVwaak7ix3WWttIW6g3xsYyuxv1My3rhpoBF3tzFQZ2WbZYK4CaLPMDXc fplV1OAcPbu6xR6vo06cn1GEpw7FS50xr789kzEeZ21zd47pLTXIoghLSzqtvCDOzn tVgmP7ONx68FhZ0pZyQk3Cf6R0XlVLk73uQSqWftvdIzGDon0EYZ5GmpN2V/f/bsZ6 xkftrPa6VaqIre2rAX57dOTHtVdpp6mLYWwEylsj3glsp6m9XgNvNxxHj0jSpUrN8Q rfjz0VQqfZxzw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9CF2718058F for ; Sun, 18 Jan 2026 11:29:21 +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,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (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 ; Sun, 18 Jan 2026 11:29:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1768735754; bh=rhJkHT71/kuOwH2ciRAt8gaDenDzNiXQt7SSivSTQkw=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=SyszT1Y76eFIc6wGWI7HXPR9SLoET1D+rKXmHLc2M+UW2p+/gE87I/UGE9bqK8r+a IIGJ5/mdY+2vrj1xZPASrPyc94LWZYB0ZaeUZm5kFO1L8IBS2MCz3LAblo1nFKAlly YNq6hbdPSqW56jgiREidhtMMANTN0tRGs567Z50JmbiNoOOp/iHq0X/xbw8pxM+Pr5 rmnUQsCu9Xamljtp7RjVfODrItvykzqfXfg4Ed5kRHwI+14Ex8qc1dvzWTkDDj3WA1 edfhNRjgJ3dVXQTM9Tcgp8qp31BR0AGcFQwob3d1t7gpiwYh6UBOhXNRXrVCvyMVgN uHWRDk2I29ejQ== Message-ID: Date: Sun, 18 Jan 2026 12:29:13 +0100 Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Subject: Re: [PHP-DEV] Proposal for functions that handle prefixes and suffixes To: Barel , PHP internals References: Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi On 1/16/26 20:16, Barel wrote: > 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 From the back of my mind I seem to remember that this was previously proposed with similar use cases, since I can remember saying the same I'm saying here before: Filenames and URLs are probably the worst possible use case for this feature, since they are “structured” strings where naively performing string operations at best just results in garbage and at worst result in security issues. > - If the domain includes a initial www part, remove it This just seems unsafe, since you are likely making assumptions about URLs you don't own. > - If the filename includes the .png extension, remove it basename($filename, ".png"); > - If the URL does not include the http:// schema, add it Simply adding a prefix will break URLs that already have a scheme. Use PHP 8.5's URI extension instead. > - If the URL is missing a trailing slash, add it > - If the URL starts with http://, change it to https:// URI extension. > - If the filename ends in .jpeg, change it to .jpg Here you probably want to have more than one mapping (e.g. .htm to .html), so you would probably use a combination of `pathinfo()` and a lookup table. > 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 This parameter order is inconsistent with both str_replace and preg_replace, which are likely the two closest existing functions we have. Naming-wise these functions should definitely start with a `str_(pre|suf)fix_` prefix for proper grouping and autocompletion. Thus: - str_prefix_add() - str_prefix_remove() - str_prefix_replace() - str_suffix_add() - str_suffix_remove() - str_suffix_replace() See also the corresponding policy: https://github.com/php/policies/blob/main/coding-standards-and-naming.rst#functions And of course Juris correctly pointed out that 'add' is misleading, because the behavior is conditional. > 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 For the reasons mentioned above, I don't see much value in the proposal. Personally I've rarely needed the functionality in the past and the cases where I needed it, it was a common enough operation that I could just place it in a function myself. Best regards Tim Düsterhus