Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #18875
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: question about preg_replace |
| Date | 2022-02-23 21:02 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <sv67d0$k2d$5@dont-email.me> (permalink) |
| References | <sv5hv3$h8m$2@gioia.aioe.org> <sv5kqr$k2d$1@dont-email.me> |
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"
Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll 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