Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122734 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 5E5C61A009C for ; Sat, 23 Mar 2024 22:58:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711234747; bh=xxagI6SBR4L5xLwh5UQ0nELRrnGRPSYmYU9SF8LeS2w=; h=References:In-Reply-To:From:Date:Subject:To:From; b=apyopTtIZ9QjWs/nZ4ZwqT0dOiscA0dDVED17tMjMPKVrdTN+SDYf1ZzGAf/fLGzF tj6WXHnryntDyquI6ICoewcGZEvcjmrzzhc+9cUtLm0hn5b2BzQN6CNvCL2O57gLyK R5zl0hnBiUAH2lzDzLi6UDsxQnqaCSnGLBmyMaQqeFmr1h+g7yYakYM0p1h85m5kK6 arhMSlBI+ehKcAsVAAYSNj0tdWvgRm+DhX/JQEwPh/PGRe3xs90Pej9e4raOZ5dK7B SNynC/TQjPzJxtBA+VztKw1iJQ4fr+nXI0gGjaMAGSgFSSOQgcV3pwsjhVSAhI5Hq9 3dhUNPGRFpRLA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 018B4180041 for ; Sat, 23 Mar 2024 22:59:04 +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.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_H2,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 mail-ed1-f49.google.com (mail-ed1-f49.google.com [209.85.208.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 ; Sat, 23 Mar 2024 22:59:00 +0000 (UTC) Received: by mail-ed1-f49.google.com with SMTP id 4fb4d7f45d1cf-56bf6418434so958312a12.0 for ; Sat, 23 Mar 2024 15:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711234716; x=1711839516; darn=lists.php.net; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=IHmJLxo9nm+E7xVqpn/eDSq8jJwzmSE3CidMZ4tCVpY=; b=jybXOubc6Cz0rez/CITYywU7T7+Mj9jSEvXUQe5otneRGbOPqzQkb2J96GAiWJqc0U KB8mooB5rVaQQ3Bp1jge1gzNFwQZbPcLqxrgCJDU9DtAqkunq2gn7qqquKpOdTVOGYWx dbe9KpKtjQPboMG103ocJr4FZdJZ8sLf3V/h/5Utuhss4Syk/uut7NiO4y9HTNNFhIEa 5rGUXYq2d8IR1wJ6VAwxnMNGfjNviEByizrwLJU5Po3T3S7wbub9f4JxmVSkqNpIasw7 jAlX38lXKLoY0+DT3xDeApxk35R589VoKePOubgqmhkE+NHsPy6Sps0AsftSzSTGStlg Tdsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711234716; x=1711839516; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IHmJLxo9nm+E7xVqpn/eDSq8jJwzmSE3CidMZ4tCVpY=; b=pLVJT1zwhQvigs9MrbVxckGasNHWf4RdabgVadv/ZMUSSNfcNNY4994CFoYvUfQbka YUglK/OAhk4dUFm3dK0dvS5olIi7TGe+ccvAR4QyAUuDuavQPGtMdGveqoTQkarUPeAc L59fhpgdkQb1hXEiwnudq6ztGjO9FiI93C7YniQiW+9j7h8O4+40QLAM5zDXKTsn+O4U tz3nSrBCyxBvT+pPsMAc6c9d47+C7VZ23ZTrKA23bzRhXolRRkxhP+MjhxOzLQZuFUmb zJfdBwqQnFsWEooojMF9/T2xDuJ7aV17GvYvVWFc/PMCDMxLHNySR7qM9d7BKdmA3x1F wQyg== X-Gm-Message-State: AOJu0Ywzk5xDWDeCb/lb8Tj954RT7n7v9dwEBXee6zDw/Wm5W9wcnSqe HruYXgb9kb/QQ6bzzr2Br6DBfY4FsYLuwvPdOIEYYFSEzmFZcKv8nE4yBUpTNF7lttUVmSFgJBs wFf6dukA+rUAZTHPX480vBjBLrpniRNJk X-Google-Smtp-Source: AGHT+IEaYQPlwjhYzmLaSgCCniue+2lNVN1c36S2Kri/A9mkqtHuMIc8xOdZFIWd0dXZZBtw6dU+yd/pz2leWSb96qw= X-Received: by 2002:a50:ef07:0:b0:568:9abe:5843 with SMTP id m7-20020a50ef07000000b005689abe5843mr1435074eds.42.1711234715834; Sat, 23 Mar 2024 15:58:35 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <3F78A125-1946-42E2-A4F5-A2B282BE2107@rwec.co.uk> <2d7ec203-6e80-445c-94f4-d29ef58743b1@rwec.co.uk> <7d53f2e4-46f3-46f1-89b0-6e1d3b0b2e32@app.fastmail.com> In-Reply-To: Date: Sun, 24 Mar 2024 08:58:24 +1000 Message-ID: Subject: [PHP-DEV] Proposal: Make $offset of substr_replace null by default To: internals Content-Type: multipart/alternative; boundary="0000000000005d5f5606145be17e" From: mickmackusa@gmail.com (mickmackusa) --0000000000005d5f5606145be17e Content-Type: text/plain; charset="UTF-8" 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. Mick mickmackusa --0000000000005d5f5606145be17e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
substr_replace() has the f= ollowing signature:

substr_replace(
= =C2=A0=C2=A0=C2=A0=C2=A0a= rray|string=C2= =A0$string,
=C2=A0=C2=A0=C2=A0=C2=A0array|stri= ng=C2=A0$replace,
=C2=A0=C2=A0=C2=A0=C2=A0array|int=C2=A0$offset,
=C2=A0=C2=A0=C2=A0=C2=A0array|int|nu= ll=C2=A0$length=C2=A0=3D=C2=A0null
= ):=C2=A0string|array

<= /div>

Was it deliberate to not= allow a null value as the third parameter?= =C2=A0 If permitted to amend this signature, I think it would be sensible t= o 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 sm= ell in multiple SO scripts that use:

$prefixed =3D preg_filter('/^/'= ;, 'prefix_', $array);

It smells because regardless of the passed in array values= 9; 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 =3D preg_replace('/^/&= #39;, 'prefix_', $array);

But wait, regex isn't even needed for this task.= =C2=A0 It can be coded more efficiently as:

$prefixed =3D=C2=A0substr_repla= ce($array, 'prefix_', 0, 0)

Next, my mind shifted to suffixing/postfixing. By using = $ in the pattern.
$prefixed =3D 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 worke= d like the $length parameter, then the langu= age would provide a native, non-regex tool for appending a static string to= all array elements.

$suffixed =3D substr_replace($array, '_suffix'= );

Finally, I wis= h to flag the observation that null values i= nside of an array are happily coerced to strings inside of the aforemention= ed functions, but null is not consumable if = singularly passed in.

So= me examples for context:=C2=A0https://3v4l.org/ENVip

I look forward to hearing feedback/concerns.

Mick
mic= kmackusa
--0000000000005d5f5606145be17e--