preg_replace_callback, carrying variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff

    preg_replace_callback, carrying variables

    I've been struggling with this for a while.

    Here's what I'm trying to do:

    private function makeMiniTemplat e($ARRAY){

    /*

    $ARRAY looks like this:
    $ARRAY['h']='Some Value';
    $ARRAY['mce']='Something else';

    */


    $template=<<<al lthis
    <div>[h]</div>
    <div>[mce]</div>
    ....

    allthis;

    /*
    I want to replace [h] with $ARRAY['h'];
    Drives me crazy not to be able to use nowdocs in classes but that's
    another story...
    */


    $template=preg_ replace_callbac k(
    "/\[(.*?)\]/s",
    create_function ('$matches','ec ho $ARRAY[$matches[1]];'),
    $template);

    return $template;
    }


    So, I don't know how to pass in $ARRAY as $ARRAY is not "scoped" inside
    the create_function . It always comes up empty.

    I don't see how to do this using callbacks either. I'm a little short of
    understanding on how this works. I've tried a number of different
    approaches.

    How do I do this?

    Jeff
  • Olaf Schinkel

    #2
    Re: preg_replace_ca llback, carrying variables

    Jeff schrieb:
    I've been struggling with this for a while.
    >
    Here's what I'm trying to do:
    >
    private function makeMiniTemplat e($ARRAY){
    >
    /*
    >
    $ARRAY looks like this:
    $ARRAY['h']='Some Value';
    $ARRAY['mce']='Something else';
    >
    */
    >
    >
    $template=<<<al lthis
    <div>[h]</div>
    <div>[mce]</div>
    ...
    >
    allthis;
    >
    /*
    I want to replace [h] with $ARRAY['h'];
    Drives me crazy not to be able to use nowdocs in classes but that's
    another story...
    */
    >
    >
    $template=preg_ replace_callbac k(
    "/\[(.*?)\]/s",
    create_function ('$matches','ec ho $ARRAY[$matches[1]];'),
    $template);
    >
    return $template;
    }
    >
    >
    So, I don't know how to pass in $ARRAY as $ARRAY is not "scoped" inside
    the create_function . It always comes up empty.
    >
    I don't see how to do this using callbacks either. I'm a little short of
    understanding on how this works. I've tried a number of different
    approaches.
    >
    How do I do this?
    >
    Jeff
    Maybe I´m blind :-) Or I don´t understand your question....
    But to replace ALL [h] with $a:
    $t = str_replace("[h]", $a, $t);



    Comment

    • Jeff

      #3
      Re: preg_replace_ca llback, carrying variables

      Olaf Schinkel wrote:
      Jeff schrieb:
      > I've been struggling with this for a while.
      >>
      > Here's what I'm trying to do:
      >>
      >private function makeMiniTemplat e($ARRAY){
      >>
      >/*
      >>
      >$ARRAY looks like this:
      >$ARRAY['h']='Some Value';
      >$ARRAY['mce']='Something else';
      >>
      >*/
      >>
      >>
      >$template=<<<a llthis
      ><div>[h]</div>
      ><div>[mce]</div>
      >...
      >>
      >allthis;
      >>
      >/*
      >I want to replace [h] with $ARRAY['h'];
      >Drives me crazy not to be able to use nowdocs in classes but that's
      >another story...
      >*/
      >>
      >>
      >$template=preg _replace_callba ck(
      > "/\[(.*?)\]/s",
      > create_function ('$matches','ec ho $ARRAY[$matches[1]];'),
      > $template);
      >>
      >return $template;
      >}
      >>
      >>
      >So, I don't know how to pass in $ARRAY as $ARRAY is not "scoped"
      >inside the create_function . It always comes up empty.
      >>
      >I don't see how to do this using callbacks either. I'm a little short
      >of understanding on how this works. I've tried a number of different
      >approaches.
      >>
      > How do I do this?
      >>
      > Jeff
      >
      Maybe I´m blind :-) Or I don´t understand your question....
      But to replace ALL [h] with $a:
      I'd like to replace [h] with $a['h'], similarly [p] with $a['p']...

      Jeff
      $t = str_replace("[h]", $a, $t);
      >
      >
      >

      Comment

      • Michael Fesser

        #4
        Re: preg_replace_ca llback, carrying variables

        ..oO(Jeff)
        >I'd like to replace [h] with $a['h'], similarly [p] with $a['p']...
        Quick 'n dirty:

        <?php
        $template = 'some [foo] stuff';
        $values = array(
        'foo' ='bar'
        );

        function replace($matche s) {
        global $values;
        $name = $matches[1];
        return isset($values[$name]) ? $values[$name] : '';
        }

        $result = preg_replace_ca llback('/\[(.+?)]/', 'replace', $template);
        var_dump($resul t);
        ?>

        Micha

        Comment

        • Jeff

          #5
          Re: preg_replace_ca llback, carrying variables

          Michael Fesser wrote:
          .oO(Jeff)
          >
          >I'd like to replace [h] with $a['h'], similarly [p] with $a['p']...
          >
          Quick 'n dirty:
          >
          <?php
          $template = 'some [foo] stuff';
          $values = array(
          'foo' ='bar'
          );
          >
          function replace($matche s) {
          global $values;
          Thanks. I had given up just prior to reading your post and had already
          gone "global". I'd also determined that it couldn't be done without a
          callback (at least I couldn't!).

          Jeff
          $name = $matches[1];
          return isset($values[$name]) ? $values[$name] : '';
          }
          >
          $result = preg_replace_ca llback('/\[(.+?)]/', 'replace', $template);
          var_dump($resul t);
          ?>
          >
          Micha

          Comment

          Working...