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


Groups > comp.lang.php > #18904

Re: question about

From "J.O. Aho" <user@example.net>
Newsgroups comp.lang.php
Subject Re: question about
Date 2022-04-04 14:26 +0200
Message-ID <jb06eqFiv7oU1@mid.individual.net> (permalink)
References <sv5hqt$h8m$1@gioia.aioe.org>

Show all headers | View raw


On 23/02/2022 15.54, 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?

Why I'm not sure, but figured out two solutions:

You set a number a limit to the replacement


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

or you can tell that it's from the beginning of the row

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

I would guess this has to do with the end of char array \0 (internally) 
and it's counted as a white space, so it's not included in the first 
match, but then as the second match, but that is just a wild guess.

-- 

  //Aho

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


Thread

question about alex <1j9448a02@lnx159sneakemail.com.invalid> - 2022-02-23 15:54 +0100
  Re: question about Kristjan Robam <he6655442211@hotmail.com> - 2022-04-04 04:06 -0700
  Re: question about "J.O. Aho" <user@example.net> - 2022-04-04 14:26 +0200
    Re: question about Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-04-04 13:07 +0000
      Re: question about Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-04-04 13:29 +0000

csiph-web