This is a discussion on PHP, RegEx and Objects within the PHP Language forums, part of the PHP Programming Forums category; > I've been playing around with my code: > > <pre><? > class Image{ > function ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
> 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" http://www.point52.com/ "Computers are like air conditioners, They don't work when you open windows" |
|
|||
|
Alexandre Lahure <admin@point52.com> wrote in message news:<opryfb14hcyq0v8u@news.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. |