Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122736 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 qa.php.net (Postfix) with ESMTPS id 42CF21A009C for ; Sun, 24 Mar 2024 14:01:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711288922; bh=w0UyJEGSovGU6IQdBACZJYMgiESFvZkbGtmd4JYs/iU=; h=Date:To:From:Subject:From; b=ifYyza4KYuVXarmhkwoK9VW/Usm5hAv582YEeH2vXaBWk8IdHmQRpQBBJaH0DU24U /EZpOtUMzfyWK3xYuILMR0JjgF053MYRu+dlLVuPW1elW62a4dFiI+9BN3wyA0bWIb xDSoUgOs+GJV8xLVoMVuEtWMitwKGeHUeg1/yvdEZjqLz5KjjeMlrauRYqupdTCwrn icqsL0oyOwY0PLNoY1tZLBh/rdrS2G/2/UAKoP54P3N67vDuito7nqJYKZ5m65Gop2 f2Ut0oX6vlFr6TXCszb49AGcTyEssJwzndmvlP+vOoXq/HFeEnV+exQIHNWU2N9Q2r +dDHxeqY6Kkag== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DE143180070 for ; Sun, 24 Mar 2024 14:02:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh1-smtp.messagingengine.com (fhigh1-smtp.messagingengine.com [103.168.172.152]) (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, 24 Mar 2024 14:02:00 +0000 (UTC) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 7BC0111400D0 for ; Sun, 24 Mar 2024 10:01:36 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Sun, 24 Mar 2024 10:01:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:message-id:mime-version:reply-to:subject :subject:to:to; s=fm2; t=1711288896; x=1711375296; bh=yC4OdXi8Tl zoafqut7pIzciQb/nmEeqG0JbVnoPZodY=; b=bC2zj3jzc51CcBLjlh1+Y19/vB EgBfcA56ZsanfoSh0xycGXEx0I5YjOYMQla5BfeVvNtPeA2m+m0DW/2kbJxpZsKv bPeeTrr8JK/y7BRtiXVcHtfZ+dXpxhAhtGCt0ZZoXeytE7pDdHN0T0eOLa1IX96Z PSXHJRDv98OoFJBY3EMPZagt8T/E7uixI9QF3T0z+oEHQw3Z6QpnllKzfDNPl6kB E/ZTkJSp51sa3+qJ3n4DR2v/xyQIm9rM64XxAxuis78lI+DD3HlrLDqGFJ66s5oZ E/CMsrf2pHnMbsbLKDeXb5Jgan8J/LnsPqtOmBNewkaLjiooqmySj7rk8FaA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1711288896; x=1711375296; bh=yC4OdXi8Tlzoafqut7pIzciQb/nm EeqG0JbVnoPZodY=; b=Gxv+eh0Z+9S3N3VZU7DKmwqoZM46PoictAa+2ARvP1EG NWvGPO0HXZYG/j3w/aIKOekH0oOv2sTCVCDtE4RG7GgHJgFmiDzrPECWiGFUSM2u +C1Q1Opsii9oIHPkLJAs/D/+ixFFcfmXRvDI4HQ6YgezZMF7KPJs7omgzvWR63Ww hVjhaTZT+8XrI9AJTTBdp0/M9uMq5lg0tQM+WYtUs6obmmR1y7K1W0jYFJLSpBru a5RBg11JkAoMelE8owg7552mB7Yj0BSeGpNw5lFqQ3yHgBr0wXztYbgKMl063OTP KHaj+Cb3WW5yCrDUo/VuKFhWJK6jqUSUMZwMAarr9A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddtjedgiedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne goufhushhpvggtthffohhmrghinhculdegledmnecujfgurhepkfffgggfvffhufgtgfes thekredttddvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhsucglkffoufhorf gnfdcuoehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecuggftrfgrthhtvghr nhepleelveegudeiieeuudegteefffegtefftdefiefgheektdegffefveekgfetkedtne cuffhomhgrihhnpeefvheglhdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgr rhgrmhepmhgrihhlfhhrohhmpehimhhsohhprdhphhhpsehrfigvtgdrtghordhukh X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sun, 24 Mar 2024 10:01:35 -0400 (EDT) Message-ID: <811bc9e0-4b24-4cf8-8204-1d8ff3dfad71@rwec.co.uk> Date: Sun, 24 Mar 2024 14:01:32 +0000 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-GB To: PHP Internals Subject: Re: [PHP-DEV] Proposal: Make $offset of substr_replace null by default Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") [Aside: please don't use "reply" when starting a new thread. Although GMail and its imitators frequently ignore it, a reply contains a header telling clients where to add it to an existing thread. I've pasted your full text into a new e-mail rather than replying, so it reliably shows as its own thread.] On 23/03/2024 22:58, mickmackusa wrote: > substr_replace() has the following signature: > > substr_replace( >     array|string $string, >     array|string $replace, >     array|int $offset, >     array|int|null $length = null > ): string|array > > Was it deliberate to not allow a null value as the third parameter?  If permitted to amend this signature, I think it would be sensible to set null as the default value for $offset and adopt the same logic as the $length parameter. > > I have recently stumbled upon what I assume is a code smell in multiple SO scripts that use: > > $prefixed = preg_filter('/^/', 'prefix_', $array); > > It smells because regardless of the passed in array values' types, there will always be a starting position of each values which are coerced to strings. In other words, the destructive feature of preg_filter() is never utilized. > > This means that for this task, preg_filter() can be unconditionally replaced with preg_replace(). > > $prefixed = preg_replace('/^/', 'prefix_', $array); > > But wait, regex isn't even needed for this task.  It can be coded more efficiently as: > > $prefixed = substr_replace($array, 'prefix_', 0, 0) > > Next, my mind shifted to suffixing/postfixing. By using $ in the pattern. > > $prefixed = preg_replace('/$/', 'prefix_', $array); > > However, there isn't a convenient way to append a string to each value using substr_replace() with the current signature. > > If the $offset parameter worked like the $length parameter, then the language would provide a native, non-regex tool for appending a static string to all array elements. > > $suffixed = substr_replace($array, '_suffix'); > > Finally, I wish to flag the observation that null values inside of an array are happily coerced to strings inside of the aforementioned functions, but null is not consumable if singularly passed in. > > Some examples for context: https://3v4l.org/ENVip > > I look forward to hearing feedback/concerns. Not being familiar with the variations supported by substr_replace, it took me a while to understand what was being proposed here. In case anyone else is similarly lost, a null $length is equivalent to strlen($string), meaning "replace to the end"; so a null $offset having the same meaning would give "append to the end". On its own, this would be pretty pointless: $foo = substr_replace('abc', 'xyz', null); // a long-winded way of writing $foo = 'abc' . 'xyz'; But the function also has built-in mapping over arrays, so it could be used to append the same string to multiple inputs: $foo = substr_replace(['hello', 'goodbye'], '!', null); Or append each entry from one list onto each entry in the other: $foo = substr_replace(['one', 'two'], [' - uno', ' - dos'], null); Demo: https://3v4l.org/6eEIG While I can see the logic, it would never occur to me to use any of the functions mentioned for this task, rather than using array_map and a regular concatenation: $foo = array_map(fn($string) => $string . '!', ['hello', 'goodbye']); $foo = array_map(fn($string, $suffix) => $string . $suffix, ['one', 'two'], [' - uno', ' - dos']); Which of course extends to more complex cases: $foo = array_map(fn($english, $spanish) => "'$english' en Español es '$spanish'", ['one', 'two'], ['uno', 'dos']); $foo = array_map(fn($english, $spanish, $german) => "$english - $spanish - $german", ['one', 'two'], ['uno', 'dos'], ['ein', 'zwei']); https://3v4l.org/d55kT So, I'm not opposed to the change, but its value seems marginal. Regards, -- Rowan Tommins [IMSoP]