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" http://www.point52.com/
"Computers are like air conditioners,
They don't work when you open windows" 11 3140
*** 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
--
> <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" http://www.point52.com/
"Computers are like air conditioners,
They don't work when you open windows"
*** 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
--
> <? 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($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" http://www.point52.com/
"Computers are like air conditioners,
They don't work when you open windows"
*** 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_ta g); /* 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
--
> 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 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" http://www.point52.com/
"Computers are like air conditioners,
They don't work when you open windows"
Alexandre Lahure <ad***@point52. com> wrote in message news:<op******* *******@news.wa nadoo.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($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" />' */
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]');
?>
> 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]');
?>
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" http://www.point52.com/
"Computers are like air conditioners,
They don't work when you open windows"
*** 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 parse 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
-- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Xah Lee |
last post by:
http://python.org/doc/2.4.1/lib/module-re.html
http://python.org/doc/2.4.1/lib/node114.html
---------
QUOTE
The module defines several functions, constants, and an exception. Some
of the functions are simplified versions of the full featured methods
for compiled regular expressions. Most non-trivial applications always
use the compiled form
UNQUOTE
|
by: Bill Cohagan |
last post by:
I'm looking for help with a regular expression question, so my first
question is which newsgroup is the best one to post to? Just in case *this*
is the best choice, here's the problem:
I'm trying to "parse" something that looks like a command line; e.g.,
op arg1, arg2, ..., argn
The individual parts (op, arg1, ...) can be matched with a \w+ pattern --
except that the args *might* be quoted to cover the case where they contain
|
by: Jon Maz |
last post by:
Hi All,
Am getting frustrated trying to port the following (pretty simple) function
to CSharp. The problem is that I'm lousy at Regular Expressions....
//from http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800
function fxnParseIt()
{
var sInputString = 'asp and database';
|
by: jeevankodali |
last post by:
Hi
I have an .Net application which processes thousands of Xml nodes each
day and for each node I am using around 30-40 Regex matches to see if
they satisfy some conditions are not. These Regex matches are called
within a loop (like if or for). E.g.
for(int i = 0; i < 10; i++)
{
Regex r = new Regex();
|
by: Frank |
last post by:
Hi,
I'm beginning to understand regex (regular expressions), but I think there's
a better way to do the next thing. I use 2 steps, is it possible to do it in
1 step?
<img somestuff src="found.gif" otherstuff>
Is it possible to get the index and length of 'found.gif' in 1 regex match?
Thanks in advance
| |
by: Talin |
last post by:
I've run in to this problem a couple of times. Say I have a piece of
text that I want to test against a large number of regular expressions,
where a different action is taken based on which regex successfully
matched. The naive approach is to loop through each regex, and stop
when one succeeds. However, I am finding this to be too slow for my
application -- currently 30% of the run time is being taken up in the
regex matching.
I thought...
|
by: ankit |
last post by:
I want to get the start and end of all the patterns mattched in regex.
I know I can get it with start() and end() fn of matched objects. But
re.search() return the match object of first matching regex in the
string. I want all match objects in that string
Here is the string :
tmplstr = """
${name}
|
by: jlowery |
last post by:
I'm looking through the tools/scripts folder from the python install,
trying to get reacquanted with the language. Got a question on the
following classfix.py snippet:
# This expression doesn't catch *all* class definition headers,
# but it's pretty darn close.
classexpr = '^\(*class ++\) *( *) *\(\(=.*\)?\):'
classprog = regex.compile(classexpr)
Since the classexpr isn't a raw string (not r prefix), doesn't the \t
|
by: Maqsood Ahmed |
last post by:
Hello,
I am trying to create a Regex object which can match ASCII character
0x05 in a given string. I have written following code to accomplish
this:
System.Text.RegularExpressions.Regex regex = new
System.Text.RegularExpressions.Regex("*?",
System.Text.RegularExpressions.RegexOptions.Compiled);
|
by: Medardo Rodriguez |
last post by:
On Fri, Aug 22, 2008 at 11:24 AM, Dan <redalastor@gmail.comwrote:
You can call *grep* posix utility.
But if the regex's matches are possible only inner the context of a
line of that file:
#<code>
res =
with file(filename) as f:
for line in f:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |