PHP, RegEx and Objects

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


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 11-12-2003
Alexandre Lahure
 
Posts: n/a
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"
http://www.point52.com/

"Computers are like air conditioners,
They don't work when you open windows"
Reply With Quote
  #12 (permalink)  
Old 11-15-2003
Stefan Farnik
 
Posts: n/a
Default Re: PHP, RegEx and Objects

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:55 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0