Connecting Tech Pros Worldwide Help | Site Map

preg_replace and back reference

  #1  
Old July 17th, 2005, 03:35 AM
Richard B. Christ
Guest
 
Posts: n/a
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
  #2  
Old July 17th, 2005, 03:35 AM
Pedro Graca
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with Google-like Autosuggest pjamrisk answers 2 December 10th, 2007 11:02 AM
Help with pdf to text noodle_snacks answers 1 September 5th, 2006 12:55 AM
Regular Expression: Backreference Inside Look-ahead Clint Pachl answers 2 July 17th, 2005 09:55 AM
Regex question...? bc90021 answers 4 July 17th, 2005 03:40 AM