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


Groups > comp.lang.php > #18874

Re: question about preg_replace

From alex <1j9448a02@lnx159sneakemail.com.invalid>
Newsgroups comp.lang.php
Subject Re: question about preg_replace
Date 2022-02-23 20:19 +0100
Organization Aioe.org NNTP Server
Message-ID <sv61bc$lqm$1@gioia.aioe.org> (permalink)
References <sv5hv3$h8m$2@gioia.aioe.org> <sv5kqr$k2d$1@dont-email.me>

Show all headers | View raw


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

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


Thread

question about preg_replace alex <1j9448a02@lnx159sneakemail.com.invalid> - 2022-02-23 15:56 +0100
  Re: question about preg_replace Mateusz Viste <mateusz@xyz.invalid> - 2022-02-23 16:33 +0100
    Re: question about preg_replace alex <1j9448a02@lnx159sneakemail.com.invalid> - 2022-02-23 16:45 +0100
      Re: question about preg_replace Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-02-23 15:49 +0000
        Re: question about preg_replace Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-02-23 15:51 +0000
          Re: question about preg_replace alex <1j9448a02@lnx159sneakemail.com.invalid> - 2022-02-23 20:10 +0100
      Re: question about preg_replace Mateusz Viste <mateusz@xyz.invalid> - 2022-02-23 18:06 +0100
        Re: question about preg_replace alex <1j9448a02@lnx159sneakemail.com.invalid> - 2022-02-23 20:14 +0100
  Re: question about preg_replace Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-02-23 15:45 +0000
    Re: question about preg_replace alex <1j9448a02@lnx159sneakemail.com.invalid> - 2022-02-23 20:19 +0100
    Re: question about preg_replace Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-02-23 21:02 +0000

csiph-web