Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.php > #4960

Re: Ist hier noch jemand? str_replace multiple replacement

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Arno Welzel <usenet@arnowelzel.de>
Newsgroups de.comp.lang.php
Subject Re: Ist hier noch jemand? str_replace multiple replacement
Date Fri, 8 Nov 2024 17:29:11 +0100
Lines 42
Message-ID <lp6sinFpfr7U1@mid.individual.net> (permalink)
References <vgavv1$5s7v$1@solani.org> <lp48b3FdaejU1@mid.individual.net> <vgkov9$l2e0$1@solani.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Trace individual.net S3KJb8BI3soTjsb9tMzSXwzzHwMW3ojybd5u8TeXWkwaxwuqLV
Cancel-Lock sha1:GMtPkSMB+TB+KInI3g44E5rEyaY= sha256:sYmOw/I3A/zrVRLwz8rQTAXfTkUWNvzvbXJ5utfDeeg=
Content-Language de-DE
In-Reply-To <vgkov9$l2e0$1@solani.org>
Xref csiph.com de.comp.lang.php:4960

Show key headers only | View raw


Detlef Paschke, 2024-11-08 11:25:

> Am 07.11.2024 um 17:31 schrieb Arno Welzel:
>> Detlef Paschke, 2024-11-04 18:23:
>>
>>
>> [...]
>>> So funktioniert es zwar, aber schön ist das auch nicht.
>>>
>>> str_replace(['./', '../', '\\'], ['','','/'], $datei)
>>
>> Was ist daran "nicht schön?".
> 
> Schöner fände ich das Ersetzungs-Array so, ['','/'] wie es bei
> preg_replace in allen Varianten geht. In einer Variante eben auch mit

Nein, preg_replace() arbeitet auch nicht so.

Zitat aus <https://www.php.net/manual/en/function.preg-replace.php>:

If there are fewer elements in the replacement array than in the pattern
array, any extra patterns will be replaced by an empty string.

(Zitat Ende)

Das wäre dann für ['', '/'] als replacement:

'./' --> '';
'../' --> '/';
'\\' --> '';

Denn ab dem 3. Suchmuster wird nur '' als Ersetzung verwendet.

> logischen ODER Operator, ['/(\.\/|\.\.\/)/', '/(\\\\)/'] und ich hoffte,
> dass sich das auf str_replace übertragen lässt. Dem ist aber wohl nicht so.

Nein, genau deswegen gibt es ja preg_replace(), weil das "oder" halt ein
Teil des regulären Ausdrucks wäre.

-- 
Arno Welzel
https://arnowelzel.de

Back to de.comp.lang.php | Previous | NextPrevious in thread | Find similar


Thread

Ist hier noch jemand? str_replace multiple replacement Detlef Paschke <schabau@t-online.de> - 2024-11-04 18:23 +0100
  Re: Ist hier noch jemand? str_replace multiple replacement Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2024-11-04 22:56 +0000
    Re: Ist hier noch jemand? str_replace multiple replacement Detlef Paschke <schabau@t-online.de> - 2024-11-05 10:03 +0100
      Re: Ist hier noch jemand? str_replace multiple replacement Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2024-11-05 09:59 +0000
        Re: Ist hier noch jemand? str_replace multiple replacement Detlef Paschke <schabau@t-online.de> - 2024-11-05 12:38 +0100
          Re: Ist hier noch jemand? str_replace multiple replacement Detlef Paschke <schabau@t-online.de> - 2024-11-05 12:55 +0100
          Re: Ist hier noch jemand? str_replace multiple replacement Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2024-11-05 12:19 +0000
            Re: Ist hier noch jemand? str_replace multiple replacement Detlef Paschke <schabau@t-online.de> - 2024-11-05 15:27 +0100
  Re: Ist hier noch jemand? str_replace multiple replacement Arno Welzel <usenet@arnowelzel.de> - 2024-11-07 17:31 +0100
    Re: Ist hier noch jemand? str_replace multiple replacement Detlef Paschke <schabau@t-online.de> - 2024-11-08 11:25 +0100
      Re: Ist hier noch jemand? str_replace multiple replacement Arno Welzel <usenet@arnowelzel.de> - 2024-11-08 17:29 +0100

csiph-web