Connecting Tech Pros Worldwide Forums | Help | Site Map

preg_replace and back reference

Richard B. Christ
Guest
 
Posts: n/a
#1: Jul 17 '05
I wrote the following code and it does NOT seem to work.

$search = array('/<popup[^>]*>/ie');
$replace = array('make_popup(split_tag(\\0))');
preg_replace($search, $replace, $someText);

If I try the following code, then the replacing seems to work (I just try to get a
dummy back reference \\1)

$search = array('/<p(op)up[^>]*>/ie');
$replace = array('make_popup(split_tag(\\1))');
preg_replace($search, $replace, $someText);

What is wrong in the first part? Thanks and cheers,

richard

Pedro Graca
Guest
 
Posts: n/a
#2: Jul 17 '05

re: preg_replace and back reference


Richard B. Christ wrote:[color=blue]
> I wrote the following code and it does NOT seem to work.
>
> $search = array('/<popup[^>]*>/ie');
> $replace = array('make_popup(split_tag(\\0))');
> preg_replace($search, $replace, $someText);
>
> If I try the following code, then the replacing seems to work (I just try to get a
> dummy back reference \\1)
>
> $search = array('/<p(op)up[^>]*>/ie');
> $replace = array('make_popup(split_tag(\\1))');
> preg_replace($search, $replace, $someText);
>
> What is wrong in the first part? Thanks and cheers,[/color]

Why do you need the /e modifier to preg_replace()?

The first snippet will try to evaluate
make_popup(split_tag(<popup parm1="value1" parm2="value2">))
as if it was php code.

Maybe you want to evaluate
make_popup(split_tag('<popup parm1="value1" parm2="value2">'))
// __________________^_______________________________ ______^__
this instead.


The second snippet (which is also wrong!!) will try to evaluate
make_popup(split_tag(op))

which php will convert (with a warning) to
make_popup(split_tag('op'))



So ... turn on all warnings:
<?php error_reporting(E_ALL); ?>

And check the result of preg_replace without the /e modifier first.



Happy Coding :-)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Closed Thread