$ 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?
Il 23/02/22 16:33, Mateusz Viste ha scritto:
php -r "echo preg_replace('#^(.*)$#', 'start-\0-end', 'center')Warning: preg_replace(): No ending delimiter '#' found in Command line
.PHP_EOL;"
code on line 1
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')Warning: preg_replace(): No ending delimiter '#' found in Command line
.PHP_EOL;"
code on line 1
~ $ php -r "echo preg_replace('#(^.*)#', 'start-\0-end', 'center') . PHP_EOL;"
start-center-end
$ 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?
php -r "echo preg_replace('#^(.*)$#', 'start-\0-end', 'center') .PHP_EOL;"
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
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')Warning: preg_replace(): No ending delimiter '#' found in Command line
.PHP_EOL;"
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.
That's probably your shell replacing the '$#' pair with nothing.
Escaping the dollar (\$) should help.
Mateusz
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
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?
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')
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 349 |
Nodes: | 16 (2 / 14) |
Uptime: | 119:54:56 |
Calls: | 7,612 |
Files: | 12,787 |
Messages: | 5,684,116 |