PHP, RegEx and Objects

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

    PHP, RegEx and Objects

    Hi all

    I'd like to execute a piece of code when I find a particular string. So I
    used preg_replace('/my_regex/e', 'my_piece_of_co de', $my_string)

    Actually, I'd like to convert something like this :
    [img-32]
    into something like that :
    <img alt="" height="600" src="image.jpg" width="800" />

    This wouldn't be very complicated if I hadn't to initialize an Image
    object, then use a method called display(). My piece of code looks like
    that :
    $img = new Image(32); $img->display();

    But instead of writing my tag (and displaying the image), PHP writes
    'Object'
    I'm looking for answers to this fundamental question : "Why ?" and of
    course if somebody can solve my problem...

    Thanks to all that are going to help me


    --
    Alexandre Lahure
    Point 52, Solutions Internet "Ready to Start"
    Call 210-825-0924 Hours Monday - Saturday By Appointment


    "Computers are like air conditioners,
    They don't work when you open windows"
  • Alvaro G Vicario

    #2
    Re: PHP, RegEx and Objects

    *** Alexandre Lahure wrote/escribió (Wed, 05 Nov 2003 20:06:26 +0100):[color=blue]
    > $img = new Image(32); $img->display();
    >
    > But instead of writing my tag (and displaying the image), PHP writes
    > 'Object'[/color]

    <IMG> tag expects text (the name of the file). What does display() method
    return?

    --
    --
    -- Álvaro G. Vicario - Burgos, Spain
    --

    Comment

    • Alexandre Lahure

      #3
      Re: PHP, RegEx and Objects

      > <IMG> tag expects text (the name of the file). What does display() method[color=blue]
      > return?[/color]

      I know that. Don't worry about what the display() method returns (but, if
      you really want to know, it displays a fully functionnal <img> tag, with
      all necessary attributes), it works just fine (actually, it was tested
      under different conditions without problems).

      The following piece of code works well in every case tested but the
      preg_replace() case (and that's what gives me headache)
      $img = new Image(32);
      $img->display();

      PHP seems to fail at Object initialization, or preg_replace() doesn't
      consider Object initialization as valid PHP code (???)


      --
      Alexandre Lahure
      Point 52, Solutions Internet "Ready to Start"
      Call 210-825-0924 Hours Monday - Saturday By Appointment


      "Computers are like air conditioners,
      They don't work when you open windows"

      Comment

      • Alvaro G Vicario

        #4
        Re: PHP, RegEx and Objects

        *** Alexandre Lahure wrote/escribió (Thu, 06 Nov 2003 14:04:56 +0100):[color=blue]
        > The following piece of code works well in every case tested but the
        > preg_replace() case (and that's what gives me headache)
        > $img = new Image(32);
        > $img->display();
        >
        > PHP seems to fail at Object initialization, or preg_replace() doesn't
        > consider Object initialization as valid PHP code (???)[/color]

        <?
        class Image{
        function display(){
        return 'foo boo foo';
        }
        }
        $img = new Image();
        echo preg_replace('/boo/', 'this works for me', $img->display());
        ?>

        --
        --
        -- Álvaro G. Vicario - Burgos, Spain
        --

        Comment

        • Alexandre Lahure

          #5
          Re: PHP, RegEx and Objects

          > <?[color=blue]
          > class Image{
          > function display(){
          > return 'foo boo foo';
          > }
          > }
          > $img = new Image();
          > echo preg_replace('/boo/', 'this works for me', $img->display());
          > ?>[/color]

          <?php
          class Image {
          function Image($image_id )
          {
          $this->image_id = $image_id;
          $query = "SELECT width, height, src FROM images WHERE
          img_id = '$this->image_id';";
          $result = mysql_query($qu ery);
          if ($row = mysql_fetch_row ($result)) {
          $this->width = $row[0];
          $this->height = $row[1];
          $this->src = $row[2];
          }
          mysql_free_resu lt($result);
          }

          function display() {
          $image_tag = "<img src=\"$this->src\" width=\"$this->width\"
          height=\"$this->height\" />";
          return $image_tag;
          }
          }

          $image_tag = preg_replace('/\[img-([0-9]+)\]/e', '\$img = new Image($1);
          \$img->display();', '[img-32]');

          print($image_ta g); /* displays 'Object'
          * instead of '<img src="image.jpg" width="800"
          height="600" />' */


          I simplified the code but this would be OK.

          --
          Alexandre Lahure
          Point 52, Solutions Internet "Ready to Start"
          Call 210-825-0924 Hours Monday - Saturday By Appointment


          "Computers are like air conditioners,
          They don't work when you open windows"

          Comment

          • Alvaro G Vicario

            #6
            Re: PHP, RegEx and Objects

            *** Alexandre Lahure wrote/escribió (Fri, 07 Nov 2003 09:46:25 +0100):[color=blue]
            > $image_tag = preg_replace('/\[img-([0-9]+)\]/e', '\$img = new Image($1);
            > \$img->display();', '[img-32]');
            >
            > print($image_ta g); /* displays 'Object'
            > * instead of '<img src="image.jpg" width="800"
            > height="600" />' */[/color]

            The '=' operator has a return value: the value itself you're assigning. For
            example:

            $age=33; // returns 33
            echo ($age=33) // prints 33

            Since new Image() returns an object, you are inserting an object within a
            string. PHP does its best to handle that: it makes a string representation
            of the object: "Object".


            --
            --
            -- Álvaro G. Vicario - Burgos, Spain
            --

            Comment

            • Alexandre Lahure

              #7
              Re: PHP, RegEx and Objects

              > The '=' operator has a return value: the value itself you're assigning.[color=blue]
              > For
              > example:
              >
              > $age=33; // returns 33
              > echo ($age=33) // prints 33
              >
              > Since new Image() returns an object, you are inserting an object within a
              > string. PHP does its best to handle that: it makes a string
              > representation
              > of the object: "Object".[/color]

              I agree, but if you look closer at the PHP manual, here what you will see
              (PCRE / preg_replace section) :

              "/e modifier makes preg_replace() treat the replacement parameter as PHP
              code after the appropriate references substitution is done. Tip: make sure
              that replacement constitutes a valid PHP code string, otherwise PHP will
              complain about a parse error at the line containing preg_replace(). "

              As long as I know, my PHP code is valid since it works well when executed
              separately. And admitting that PHP outputs 'Object', why does it not
              output the second part of the code ?

              --
              Alexandre Lahure
              Point 52, Solutions Internet "Ready to Start"
              Call 210-825-0924 Hours Monday - Saturday By Appointment


              "Computers are like air conditioners,
              They don't work when you open windows"

              Comment

              • Stefan Farnik

                #8
                Re: PHP, RegEx and Objects

                Alexandre Lahure <admin@point52. com> wrote in message news:<oprx9ennb lyq0v8u@news.wa nadoo.fr>...[color=blue]
                > <?php
                > class Image {
                > function Image($image_id )
                > {
                > $this->image_id = $image_id;
                > $query = "SELECT width, height, src FROM images WHERE
                > img_id = '$this->image_id';";
                > $result = mysql_query($qu ery);
                > if ($row = mysql_fetch_row ($result)) {
                > $this->width = $row[0];
                > $this->height = $row[1];
                > $this->src = $row[2];
                > }
                > mysql_free_resu lt($result);
                > }
                >
                > function display() {
                > $image_tag = "<img src=\"$this->src\" width=\"$this->width\"
                > height=\"$this->height\" />";
                > return $image_tag;
                > }
                > }
                >
                > $image_tag = preg_replace('/\[img-([0-9]+)\]/e', '\$img = new Image($1);
                > \$img->display();', '[img-32]');
                >
                > print($image_ta g); /* displays 'Object'
                > * instead of '<img src="image.jpg" width="800"
                > height="600" />' */
                >[/color]

                try something like this:

                <?php
                class Image {
                function Image($image_id )
                {
                $this->image_id = $image_id;
                $query = "SELECT width, height, src FROM images
                WHERE
                img_id = '$this->image_id';";
                $result = mysql_query($qu ery);
                if ($row = mysql_fetch_row ($result)) {
                $this->width = $row[0];
                $this->height = $row[1];
                $this->src = $row[2];
                }
                mysql_free_resu lt($result);
                }

                function display($image_ id = null)
                {
                if ($image_id !== null) {
                $img = new Image($image_id );
                return $img->display();
                } else {
                $image_tag = "<img src=\"$this->src\"
                width=\"$this->width\" height=\"$this->height\" />";
                return $image_tag;
                }
                }
                }

                $image_tag = preg_replace('/\[img-([0-9]+)\]/e',
                'Image::display ($1);', '[img-32]');

                ?>

                Comment

                • Alexandre Lahure

                  #9
                  Re: PHP, RegEx and Objects

                  > try something like this:[color=blue]
                  >
                  > <?php
                  > class Image {
                  > function Image($image_id )
                  > {
                  > $this->image_id = $image_id;
                  > $query = "SELECT width, height, src FROM images
                  > WHERE
                  > img_id = '$this->image_id';";
                  > $result = mysql_query($qu ery);
                  > if ($row = mysql_fetch_row ($result)) {
                  > $this->width = $row[0];
                  > $this->height = $row[1];
                  > $this->src = $row[2];
                  > }
                  > mysql_free_resu lt($result);
                  > }
                  >
                  > function display($image_ id = null)
                  > {
                  > if ($image_id !== null) {
                  > $img = new Image($image_id );
                  > return $img->display();
                  > } else {
                  > $image_tag = "<img src=\"$this->src\"
                  > width=\"$this->width\" height=\"$this->height\" />";
                  > return $image_tag;
                  > }
                  > }
                  > }
                  >
                  > $image_tag = preg_replace('/\[img-([0-9]+)\]/e',
                  > 'Image::display ($1);', '[img-32]');
                  >
                  > ?>[/color]

                  Yes, it works, thank you, but I can't stop thinking it's cheating. I'm
                  sure there is a way to do this "fairly", or the PHP manual is lying about
                  the power of the 'e' modifier of preg_replace().

                  One day, truth will be mine...


                  --
                  Alexandre Lahure
                  Point 52, Solutions Internet "Ready to Start"
                  Call 210-825-0924 Hours Monday - Saturday By Appointment


                  "Computers are like air conditioners,
                  They don't work when you open windows"

                  Comment

                  • Alvaro G Vicario

                    #10
                    Re: PHP, RegEx and Objects

                    *** Alexandre Lahure wrote/escribió (Fri, 07 Nov 2003 13:54:47 +0100):[color=blue]
                    > "/e modifier makes preg_replace() treat the replacement parameter as PHP
                    > code after the appropriate references substitution is done. Tip: make sure
                    > that replacement constitutes a valid PHP code string, otherwise PHP will
                    > complain about a parse error at the line containing preg_replace(). "[/color]

                    I've been playing around with my code:

                    <pre><?
                    class Image{
                    function display(){
                    return '[Here goes image tag]';
                    }
                    }
                    $img1=new Image();
                    echo preg_replace('/i/', $img1->display(), "This is a test\n");
                    echo preg_replace('/i/e', '$img1->display()', "This is a test\n");
                    echo preg_replace('/i/e', '$img2=new Image(); $img2->display();',
                    "This is a test\n")
                    ?>

                    This prints:

                    Th[Here goes image tag]s [Here goes image tag]s a test
                    Th[Here goes image tag]s [Here goes image tag]s a test
                    ThObjects Objects a test


                    Could it be a variable scope issue?

                    --
                    --
                    -- Álvaro G. Vicario - Burgos, Spain
                    --

                    Comment

                    • Alexandre Lahure

                      #11
                      Re: PHP, RegEx and Objects

                      > I've been playing around with my code:[color=blue]
                      >
                      > <pre><?
                      > class Image{
                      > function display(){
                      > return '[Here goes image tag]';
                      > }
                      > }
                      > $img1=new Image();
                      > echo preg_replace('/i/', $img1->display(), "This is a test\n");
                      > echo preg_replace('/i/e', '$img1->display()', "This is a test\n");
                      > echo preg_replace('/i/e', '$img2=new Image(); $img2->display();',
                      > "This is a test\n")
                      > ?>
                      >
                      > This prints:
                      >
                      > Th[Here goes image tag]s [Here goes image tag]s a test
                      > Th[Here goes image tag]s [Here goes image tag]s a test
                      > ThObjects Objects a test
                      >
                      >
                      > Could it be a variable scope issue?[/color]


                      I don't think so. Actually, variable initialization and method call must
                      be in the same replace pattern.

                      --
                      Alexandre Lahure
                      Point 52, Solutions Internet "Ready to Start"
                      Call 210-825-0924 Hours Monday - Saturday By Appointment


                      "Computers are like air conditioners,
                      They don't work when you open windows"

                      Comment

                      • Stefan Farnik

                        #12
                        Re: PHP, RegEx and Objects

                        Alexandre Lahure <admin@point52. com> wrote in message news:<opryfb14h cyq0v8u@news.wa nadoo.fr>...[color=blue]
                        > Yes, it works, thank you, but I can't stop thinking it's cheating. I'm
                        > sure there is a way to do this "fairly", or the PHP manual is lying about
                        > the power of the 'e' modifier of preg_replace().
                        >
                        > One day, truth will be mine...[/color]

                        I don't think so.
                        You pass two calls of functions to preg_replace, since your first
                        function returns something (object), preg_replace will use this for
                        replacement.
                        You also can write a wrapper function:

                        <?php
                        imgDisplay($img _id) {
                        $img = new Image($img_id);
                        return $img->display();
                        }
                        ?>

                        This will work, too.

                        Comment

                        Working...