Professional Web Applications Themes

PHP, RegEx and Objects - PHP Development

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_code', $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 ...

  1. #1

    Default 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_code', $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"
    [url]http://www.point52.com/[/url]

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

  2. #2

    Default Re: PHP, RegEx and Objects

    *** Alexandre Lahure wrote/escribió (Wed, 05 Nov 2003 20:06:26 +0100):
    > $img = new Image(32); $img->display();
    >
    > But instead of writing my tag (and displaying the image), PHP writes
    > 'Object'
    <IMG> tag expects text (the name of the file). What does display() method
    return?

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

  3. #3

    Default Re: PHP, RegEx and Objects

    > <IMG> tag expects text (the name of the file). What does display() method
    > return?
    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"
    [url]http://www.point52.com/[/url]

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

  4. #4

    Default Re: PHP, RegEx and Objects

    *** Alexandre Lahure wrote/escribió (Thu, 06 Nov 2003 14:04:56 +0100):
    > 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 (???)
    <?
    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
    --
    Alvaro G Vicario Guest

  5. #5

    Default Re: PHP, RegEx and Objects

    > <?
    > class Image{
    > function display(){
    > return 'foo boo foo';
    > }
    > }
    > $img = new Image();
    > echo preg_replace('/boo/', 'this works for me', $img->display());
    > ?>
    <?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($query);
    if ($row = mysql_fetch_row($result)) {
    $this->width = $row[0];
    $this->height = $row[1];
    $this->src = $row[2];
    }
    mysql_free_result($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_tag); /* 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"
    [url]http://www.point52.com/[/url]

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

  6. #6

    Default Re: PHP, RegEx and Objects

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

  7. #7

    Default Re: PHP, RegEx and Objects

    > 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".
    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 p 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"
    [url]http://www.point52.com/[/url]

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

  8. #8

    Default Re: PHP, RegEx and Objects

    Alexandre Lahure <adminpoint52.com> wrote in message news:<oprx9ennblyq0v8unews.wanadoo.fr>...
    > <?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($query);
    > if ($row = mysql_fetch_row($result)) {
    > $this->width = $row[0];
    > $this->height = $row[1];
    > $this->src = $row[2];
    > }
    > mysql_free_result($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_tag); /* displays 'Object'
    > * instead of '<img src="image.jpg" width="800"
    > height="600" />' */
    >
    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($query);
    if ($row = mysql_fetch_row($result)) {
    $this->width = $row[0];
    $this->height = $row[1];
    $this->src = $row[2];
    }
    mysql_free_result($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]');

    ?>
    Stefan Farnik Guest

  9. #9

    Default Re: PHP, RegEx and Objects

    > 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($query);
    > if ($row = mysql_fetch_row($result)) {
    > $this->width = $row[0];
    > $this->height = $row[1];
    > $this->src = $row[2];
    > }
    > mysql_free_result($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]');
    >
    > ?>
    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"
    [url]http://www.point52.com/[/url]

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

  10. #10

    Default Re: PHP, RegEx and Objects

    *** Alexandre Lahure wrote/escribió (Fri, 07 Nov 2003 13:54:47 +0100):
    > "/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 p error at the line containing preg_replace()."
    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
    --
    Alvaro G Vicario Guest

  11. #11

    Default Re: PHP, RegEx and Objects

    > 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?

    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"
    [url]http://www.point52.com/[/url]

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

  12. #12

    Default Re: PHP, RegEx and Objects

    Alexandre Lahure <adminpoint52.com> wrote in message news:<opryfb14hcyq0v8unews.wanadoo.fr>...
    > 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...
    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.
    Stefan Farnik Guest

Similar Threads

  1. Updating Objects in Objects in the Library
    By Robert_Bovasso@adobeforums.com in forum Adobe Indesign Macintosh
    Replies: 5
    Last Post: August 21st, 09:34 PM
  2. Newbie Question? Aligning Objects to other Objects?
    By grant.smith@adobeforums.com in forum Adobe Illustrator Macintosh
    Replies: 6
    Last Post: April 27th, 09:10 PM
  3. Storing Objects/Arrays in Stored Objects
    By teflon000 webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: January 24th, 07:11 PM
  4. help with objects facing other objects
    By Dark Lightning webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: January 14th, 09:00 AM
  5. Replies: 0
    Last Post: July 11th, 02:58 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139