Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Dennis Ritchie is twice as bright as Steve Jobs, and only half wrong. -- Jim Gettys


devel / comp.lang.php / question about preg_replace

SubjectAuthor
* question about preg_replacealex
+* Re: question about preg_replaceMateusz Viste
|`* Re: question about preg_replacealex
| +* Re: question about preg_replaceLew Pitcher
| |`* Re: question about preg_replaceLew Pitcher
| | `- Re: question about preg_replacealex
| `* Re: question about preg_replaceMateusz Viste
|  `- Re: question about preg_replacealex
`* Re: question about preg_replaceLew Pitcher
 +- Re: question about preg_replacealex
 `- Re: question about preg_replaceLew Pitcher

1
question about preg_replace

<sv5hv3$h8m$2@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=435&group=comp.lang.php#435

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!J/+ttyvLGPMUJZz1/1rzLg.user.46.165.242.91.POSTED!not-for-mail
From: 1j9448a02@lnx159sneakemail.com.invalid (alex)
Newsgroups: comp.lang.php
Subject: question about preg_replace
Date: Wed, 23 Feb 2022 15:56:35 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sv5hv3$h8m$2@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="17686"; posting-host="J/+ttyvLGPMUJZz1/1rzLg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: it-IT
 by: alex - Wed, 23 Feb 2022 14:56 UTC

$ echo center | sed -E 's#(.*)#start-\1-end#'
start-center-end

I tried to do the same thing with php, but the result is different

$ php -r "echo preg_replace('#(.*)#', 'start-\0-end', 'center') . PHP_EOL;"
start-center-endstart--end
^^^^^^^^^^

Why?

Re: question about preg_replace

<sv5k3d$1ee0$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=436&group=comp.lang.php#436

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!Lt209OpLGhAtWfSuEHRcrw.user.46.165.242.75.POSTED!not-for-mail
From: mateusz@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 16:33:01 +0100
Organization: . . .
Message-ID: <sv5k3d$1ee0$1@gioia.aioe.org>
References: <sv5hv3$h8m$2@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="47552"; posting-host="Lt209OpLGhAtWfSuEHRcrw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Wed, 23 Feb 2022 15:33 UTC

On 23 Feb 2022 15:56:35 +0100 alex wrote:
> $ echo center | sed -E 's#(.*)#start-\1-end#'
> start-center-end
>
> I tried to do the same thing with php, but the result is different
>
> $ php -r "echo preg_replace('#(.*)#', 'start-\0-end', 'center') .
> PHP_EOL;"
> start-center-endstart--end
>
> Why?

Looks like the preg matches two times: first your 'center' content, and
then what's left (an empty string). I can think of two ways to avoid
that:

Enforce that what's processed has at least one character:

php -r "echo preg_replace('#.(.*)#', 'start-\0-end', 'center') .PHP_EOL;"

Or make sure the preg consumes the entire line:

php -r "echo preg_replace('#^(.*)$#', 'start-\0-end', 'center') .PHP_EOL;"

Mateusz

Re: question about preg_replace

<sv5kqr$k2d$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=437&group=comp.lang.php#437

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!news.freedyn.de!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 15:45:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <sv5kqr$k2d$1@dont-email.me>
References: <sv5hv3$h8m$2@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 23 Feb 2022 15:45:31 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c0e52d0a62b858db636524364719928d";
logging-data="20557"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0PxnL4aRgk++2qKiltP9BacaTwTl/+xI="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:rcnVGdW75xVgSxVTVjrfxhxdxYw=
 by: Lew Pitcher - Wed, 23 Feb 2022 15:45 UTC

On Wed, 23 Feb 2022 15:56:35 +0100, alex wrote:

> $ echo center | sed -E 's#(.*)#start-\1-end#'
> start-center-end
>
> I tried to do the same thing with php, but the result is different
>
> $ php -r "echo preg_replace('#(.*)#', 'start-\0-end', 'center') .
> PHP_EOL;"
> start-center-endstart--end
> ^^^^^^^^^^
>
> Why?

First off, it is necessary to recognize that sed(1) uses, depending on
the options given, either POSIX basic regular expressions (BREs) or
POSIX extended regular expressions (EREs), while PHP's preg_* functions
use Perl-compatable regular expressions (PCREs). These two types of
RE (POSIX and PCRE) have differences in how they match REs, which
would explain why you get different results from sed(1) and php
preg_replace()

As for PHP preg_replace(), the function will /repeat/ the substitution
as often as possible, if you do not specify a limit. What
preg_replace('#(.*)#', 'start-\0-end', 'center')
does is
- match .* to the entire string 'center',
- because of the grouping brackets in the RE, and the reference in
the replacement string, it now replaces 'center' with
'start-center-end', and continues onward in the string. It now
- matches .* to the empty string at the end of 'center' (remember,
.* will match ZERO or more occurrences of ANY character), and
- replaces that empty string with the replacement string. It then
- runs out of string, and terminates.

You have a couple of possible changes that you can apply to
bring your PHP closer to sed(1):
1) you can limit your RE to one occurrence:
preg_replace('#(.*)#', 'start-\0-end', 'center',1)

or

2) you can anchor your RE to the start of the string:
preg_replace('#^(.*)#', 'start-\0-end', 'center')
preg_replace('#(^.*)#', 'start-\0-end', 'center')

HTH
--
Lew Pitcher
"In Skills, We Trust"

Re: question about preg_replace

<sv5kri$5og$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=438&group=comp.lang.php#438

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!J/+ttyvLGPMUJZz1/1rzLg.user.46.165.242.91.POSTED!not-for-mail
From: 1j9448a02@lnx159sneakemail.com.invalid (alex)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 16:45:53 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sv5kri$5og$1@gioia.aioe.org>
References: <sv5hv3$h8m$2@gioia.aioe.org> <sv5k3d$1ee0$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="5904"; posting-host="J/+ttyvLGPMUJZz1/1rzLg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: it-IT
 by: alex - Wed, 23 Feb 2022 15:45 UTC

Il 23/02/22 16:33, Mateusz Viste ha scritto:
> php -r "echo preg_replace('#^(.*)$#', 'start-\0-end', 'center') .PHP_EOL;"
>

Warning: preg_replace(): No ending delimiter '#' found in Command line
code on line 1

Re: question about preg_replace

<sv5l2a$k2d$3@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=439&group=comp.lang.php#439

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 15:49:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <sv5l2a$k2d$3@dont-email.me>
References: <sv5hv3$h8m$2@gioia.aioe.org> <sv5k3d$1ee0$1@gioia.aioe.org>
<sv5kri$5og$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 23 Feb 2022 15:49:31 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c0e52d0a62b858db636524364719928d";
logging-data="20557"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18j+O6dqeOBV3j+Q4YPr3R+0FsHbOVPnoU="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:oWxhIG53H/UNFPHI/CMBtwDLs60=
 by: Lew Pitcher - Wed, 23 Feb 2022 15:49 UTC

On Wed, 23 Feb 2022 16:45:53 +0100, alex wrote:

> Il 23/02/22 16:33, Mateusz Viste ha scritto:
>> php -r "echo preg_replace('#^(.*)$#', 'start-\0-end', 'center')
>> .PHP_EOL;"
>>
>>
> Warning: preg_replace(): No ending delimiter '#' found in Command line
> code on line 1

~ $ php -r "echo preg_replace('#(^.*)#', 'start-\0-end', 'center') .
PHP_EOL;"
start-center-end
~ $

--
Lew Pitcher
"In Skills, We Trust"

Re: question about preg_replace

<sv5l5b$k2d$4@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=440&group=comp.lang.php#440

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 15:51:08 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <sv5l5b$k2d$4@dont-email.me>
References: <sv5hv3$h8m$2@gioia.aioe.org> <sv5k3d$1ee0$1@gioia.aioe.org>
<sv5kri$5og$1@gioia.aioe.org> <sv5l2a$k2d$3@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 23 Feb 2022 15:51:08 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c0e52d0a62b858db636524364719928d";
logging-data="20557"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++Qhh2XVpAK1d7pyW034rFmaUv8J2Tswg="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:66CxdlwymEO1V0BWnQ13RXsIiWg=
 by: Lew Pitcher - Wed, 23 Feb 2022 15:51 UTC

On Wed, 23 Feb 2022 15:49:31 +0000, Lew Pitcher wrote:

> On Wed, 23 Feb 2022 16:45:53 +0100, alex wrote:
>
>> Il 23/02/22 16:33, Mateusz Viste ha scritto:
>>> php -r "echo preg_replace('#^(.*)$#', 'start-\0-end', 'center')
>>> .PHP_EOL;"
>>>
>>>
>> Warning: preg_replace(): No ending delimiter '#' found in Command line
>> code on line 1
>
> ~ $ php -r "echo preg_replace('#(^.*)#', 'start-\0-end', 'center') .
> PHP_EOL;"
> start-center-end

Sorry, wrong example from my testing. How about...

~ $ php -r "echo preg_replace('#^(.*)#', 'start-\0-end', 'center') .
PHP_EOL;"
start-center-end
~ $

It could be that we are running different versions of PHP.
--
Lew Pitcher
"In Skills, We Trust"

Re: question about preg_replace

<sv5pje$keu$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=441&group=comp.lang.php#441

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!Lt209OpLGhAtWfSuEHRcrw.user.46.165.242.75.POSTED!not-for-mail
From: mateusz@xyz.invalid (Mateusz Viste)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 18:06:54 +0100
Organization: . . .
Message-ID: <sv5pje$keu$1@gioia.aioe.org>
References: <sv5hv3$h8m$2@gioia.aioe.org>
<sv5k3d$1ee0$1@gioia.aioe.org>
<sv5kri$5og$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="20958"; posting-host="Lt209OpLGhAtWfSuEHRcrw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Mateusz Viste - Wed, 23 Feb 2022 17:06 UTC

On Wed, 23 Feb 2022 16:45:53 +0100
alex <1j9448a02@lnx159sneakemail.com.invalid> wrote:

> Il 23/02/22 16:33, Mateusz Viste ha scritto:
> > php -r "echo preg_replace('#^(.*)$#', 'start-\0-end', 'center')
> > .PHP_EOL;"
>
> Warning: preg_replace(): No ending delimiter '#' found in Command
> line code on line 1

That's probably your shell replacing the '$#' pair with nothing.
Escaping the dollar (\$) should help.

Mateusz

Re: question about preg_replace

<sv60ra$bih$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=442&group=comp.lang.php#442

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!J/+ttyvLGPMUJZz1/1rzLg.user.46.165.242.91.POSTED!not-for-mail
From: 1j9448a02@lnx159sneakemail.com.invalid (alex)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 20:10:34 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sv60ra$bih$1@gioia.aioe.org>
References: <sv5hv3$h8m$2@gioia.aioe.org> <sv5k3d$1ee0$1@gioia.aioe.org>
<sv5kri$5og$1@gioia.aioe.org> <sv5l2a$k2d$3@dont-email.me>
<sv5l5b$k2d$4@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="11857"; posting-host="J/+ttyvLGPMUJZz1/1rzLg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
Content-Language: it-IT
X-Notice: Filtered by postfilter v. 0.9.2
 by: alex - Wed, 23 Feb 2022 19:10 UTC

Il 23/02/22 16:51, Lew Pitcher ha scritto:
> On Wed, 23 Feb 2022 15:49:31 +0000, Lew Pitcher wrote:
>
>> On Wed, 23 Feb 2022 16:45:53 +0100, alex wrote:
>>
>>> Il 23/02/22 16:33, Mateusz Viste ha scritto:
>>>> php -r "echo preg_replace('#^(.*)$#', 'start-\0-end', 'center')
>>>> .PHP_EOL;"
>>>>
>>>>
>>> Warning: preg_replace(): No ending delimiter '#' found in Command line
>>> code on line 1
>>
>> ~ $ php -r "echo preg_replace('#(^.*)#', 'start-\0-end', 'center') .
>> PHP_EOL;"
>> start-center-end
>
> Sorry, wrong example from my testing. How about...
>
> ~ $ php -r "echo preg_replace('#^(.*)#', 'start-\0-end', 'center') .
> PHP_EOL;"
> start-center-end
> ~ $
>
> It could be that we are running different versions of PHP.

thanks

Re: question about preg_replace

<sv611t$hg5$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=443&group=comp.lang.php#443

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!J/+ttyvLGPMUJZz1/1rzLg.user.46.165.242.91.POSTED!not-for-mail
From: 1j9448a02@lnx159sneakemail.com.invalid (alex)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 20:14:04 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sv611t$hg5$1@gioia.aioe.org>
References: <sv5hv3$h8m$2@gioia.aioe.org> <sv5k3d$1ee0$1@gioia.aioe.org>
<sv5kri$5og$1@gioia.aioe.org> <sv5pje$keu$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="17925"; posting-host="J/+ttyvLGPMUJZz1/1rzLg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: it-IT
 by: alex - Wed, 23 Feb 2022 19:14 UTC

Il 23/02/22 18:06, Mateusz Viste ha scritto:
> That's probably your shell replacing the '$#' pair with nothing.
> Escaping the dollar (\$) should help.
>
> Mateusz
>

exact

Re: question about preg_replace

<sv61bc$lqm$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=444&group=comp.lang.php#444

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!J/+ttyvLGPMUJZz1/1rzLg.user.46.165.242.91.POSTED!not-for-mail
From: 1j9448a02@lnx159sneakemail.com.invalid (alex)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 20:19:08 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sv61bc$lqm$1@gioia.aioe.org>
References: <sv5hv3$h8m$2@gioia.aioe.org> <sv5kqr$k2d$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="22358"; posting-host="J/+ttyvLGPMUJZz1/1rzLg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.5.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: it-IT
 by: alex - Wed, 23 Feb 2022 19:19 UTC

Il 23/02/22 16:45, Lew Pitcher ha scritto:
> On Wed, 23 Feb 2022 15:56:35 +0100, alex wrote:
>
>> $ echo center | sed -E 's#(.*)#start-\1-end#'
>> start-center-end
>>
>> I tried to do the same thing with php, but the result is different
>>
>> $ php -r "echo preg_replace('#(.*)#', 'start-\0-end', 'center') .
>> PHP_EOL;"
>> start-center-endstart--end
>> ^^^^^^^^^^
>>
>> Why?
>
> First off, it is necessary to recognize that sed(1) uses, depending on
> the options given, either POSIX basic regular expressions (BREs) or
> POSIX extended regular expressions (EREs), while PHP's preg_* functions
> use Perl-compatable regular expressions (PCREs). These two types of
> RE (POSIX and PCRE) have differences in how they match REs, which
> would explain why you get different results from sed(1) and php
> preg_replace()
>
>
> As for PHP preg_replace(), the function will /repeat/ the substitution
> as often as possible, if you do not specify a limit. What
> preg_replace('#(.*)#', 'start-\0-end', 'center')
> does is
> - match .* to the entire string 'center',
> - because of the grouping brackets in the RE, and the reference in
> the replacement string, it now replaces 'center' with
> 'start-center-end', and continues onward in the string. It now
> - matches .* to the empty string at the end of 'center' (remember,
> .* will match ZERO or more occurrences of ANY character), and
> - replaces that empty string with the replacement string. It then
> - runs out of string, and terminates.
>
> You have a couple of possible changes that you can apply to
> bring your PHP closer to sed(1):
> 1) you can limit your RE to one occurrence:
> preg_replace('#(.*)#', 'start-\0-end', 'center',1)
>
> or
>
> 2) you can anchor your RE to the start of the string:
> preg_replace('#^(.*)#', 'start-\0-end', 'center')
> preg_replace('#(^.*)#', 'start-\0-end', 'center')
>
>
> HTH

thanks

Re: question about preg_replace

<sv67d0$k2d$5@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=445&group=comp.lang.php#445

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.php
Subject: Re: question about preg_replace
Date: Wed, 23 Feb 2022 21:02:24 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <sv67d0$k2d$5@dont-email.me>
References: <sv5hv3$h8m$2@gioia.aioe.org> <sv5kqr$k2d$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 23 Feb 2022 21:02:24 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c0e52d0a62b858db636524364719928d";
logging-data="20557"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5yQlxoYafCKUCFnLY95gNfMrod8ubuJ8="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:/TqxovoK4oY3iEzTWc34ies04ZQ=
 by: Lew Pitcher - Wed, 23 Feb 2022 21:02 UTC

On Wed, 23 Feb 2022 15:45:31 +0000, Lew Pitcher wrote:

> On Wed, 23 Feb 2022 15:56:35 +0100, alex wrote:
>
>> $ echo center | sed -E 's#(.*)#start-\1-end#'
>> start-center-end
>>
>> I tried to do the same thing with php, but the result is different
>>
>> $ php -r "echo preg_replace('#(.*)#', 'start-\0-end', 'center') .
>> PHP_EOL;"
>> start-center-endstart--end
>> ^^^^^^^^^^
>>
>> Why?

[snip]

> You have a couple of possible changes that you can apply to bring your
> PHP closer to sed(1):
> 1) you can limit your RE to one occurrence:
> preg_replace('#(.*)#', 'start-\0-end', 'center',1)
>
> or
>
> 2) you can anchor your RE to the start of the string:
> preg_replace('#^(.*)#', 'start-\0-end', 'center')
> preg_replace('#(^.*)#', 'start-\0-end', 'center')

You could also change the RE so that it matches ONE OR MORE characters,
preventing it from matching the empty string at the end of the data.
preg_replace('#(.+)#', 'start-\0-end', 'center')

--
Lew Pitcher
"In Skills, We Trust"

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor