preg_replace and back reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard B. Christ

    preg_replace and back reference

    I wrote the following code and it does NOT seem to work.

    $search = array('/<popup[^>]*>/ie');
    $replace = array('make_pop up(split_tag(\\ 0))');
    preg_replace($s earch, $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_pop up(split_tag(\\ 1))');
    preg_replace($s earch, $replace, $someText);

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

    richard
  • Pedro Graca

    #2
    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_pop up(split_tag(\\ 0))');
    > preg_replace($s earch, $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_pop up(split_tag(\\ 1))');
    > preg_replace($s earch, $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(spli t_tag(<popup parm1="value1" parm2="value2"> ))
    as if it was php code.

    Maybe you want to evaluate
    make_popup(spli t_tag('<popup parm1="value1" parm2="value2"> '))
    // _______________ ___^___________ _______________ ___________^__
    this instead.


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

    which php will convert (with a warning) to
    make_popup(spli t_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 =--

    Comment

    Working...