Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "J.O. Aho" Newsgroups: comp.lang.php Subject: Re: question about Date: Mon, 4 Apr 2022 14:26:01 +0200 Lines: 32 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net 6PS+ORLGkNnBo/8dHeHqNwau3xfO8qOJ09N5qs82wpMvhEo91q Cancel-Lock: sha1:QtNW5/rsIAUfI0H47Ab4QEEUBl4= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Content-Language: en-US-large In-Reply-To: Xref: csiph.com comp.lang.php:18904 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