Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 03:35 AM
Richard B. Christ
Guest
 
Posts: n/a
Default preg_replace and back reference

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
Default 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 =--
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.