Jump to content


This topic is now archived and is closed to further replies.


eregi_replace into function?

Recommended Posts

[code]$coded = ereg_replace("(http://)?(www.)?exam(ple)?.com/gallery/image/\?id=([0-9]+)", make_slide("\\4"), $coded);[/code]

I'm trying to pass the text in "\\4" into my function...it sorta works, but I clearly am missing some understanding about what it is I'm doing. The function looks like:

[code]function make_slide($photo) {
$slideshow = mysql_query("SELECT * FROM images WHERE image_id='".$photo."'");
if (mysql_num_rows($slideshow) > 0) {
while($row = mysql_fetch_array($slideshow)) {
$slide1 = //bunch of formatting here
return $slide1;

Here's what happens: No matter what is passed to the function, it has a stringlen of 2 and cannot be evaluated as a normal string.

Let's say "\\4" is "35000". If you had the function return this line:

[code]return $photo . " length: " . strlen($photo) . " value: " . 0 + $photo;[/code]

You'd get

[code]35000 length: 2 value: 0[/code]

What crucial misunderstanding am I laboring under? I have tried sending "" + "\\4", (string)"\\4", etc. and it always returns the same results. You can always "see" the expected string value but it's completely unavailable for manipulation, and always has a length of "2", whether it's "h" or "hhhhhhhhhhhhhhhhhhhhhhhh". It doesn't matter what "\\4" actually is, whether it's numbers or letters or both.

If I don't call a function, but instead do this:

eregi("(http://)?(www.)?exam(ple)?.com/gallery/image/\?id=([[:digit:]]+)", $coded, $slide);
if ($slide[0]) {
$photid = eregi_replace("(http://)?(www.)?exam(ple)?.com/gallery/image/\?id=([0-9]+)", "\\4", $slide[0]);
$slideshow = mysql_query("SELECT * FROM images WHERE image_id='".$photid."'");
if (mysql_num_rows($slideshow) > 0) {
while($crow = mysql_fetch_array($slideshow)) {
$slide1 .= // bunch of formatting
$coded = eregi_replace("(http://)?(www.)?exam(ple)?.com/gallery/image/\?id=".$photid, $slide1, $coded);


It works perfectly, but obviously only catches the first instance of the matching code in the text block (hence my desire to call a function). I'm obviously not an expert so any advice or insight is appreciated. Thanks!

Share this post

Link to post
Share on other sites
2 things to try....

1. pass the value as numeric instead of in the quotes (i.e. make_slide(\\4) - if it returns an error then forget that.

2. On your testing of the returned string change to this....

return $photo . " length: " . strlen($photo) . " value: " . (0 + $photo);

note the operands on the 0 + $photo (why is the 0 + in there?) there is an operator precedence problem there...

"a string " . 9 + 67

will evaluate as "a string 9" + 67 which you can't do!!!!

Share this post

Link to post
Share on other sites
Toon, thanks for the ideas. I've always been under the impression that PHP will attempt to evaluate a string as an integer if you put it in a situation where you expect it to act like one; the example I've seen is the one I gave, adding it to "0".

When you try to pass \\4 to the function, it errors on the backslashes. And the parens didn't do anything in the "value" expression. Here's another interesting data point:


Adding a string in front of the back reference. Here's the result asking to return the string and its length where \\4 = "790":

[code]howdy790 length 7[/code]

And for "14258"?

[code]howdy14258 length 7[/code]

I guess I'm really REALLY puzzled about how it can return a ten-character string (and it evaluates to "true" if you perform a test to see if it IS a string) and yet only measure the string length of whatever string you append plus two for the back reference. There is probably something really simple I'm missing here (like "you need to perform x on a back reference before you can send it to a function") but I've puzzled over this for a few hours now.

Share this post

Link to post
Share on other sites
One other "interesting" data point. If you do some substring work, this is what is returned:

for backreference \4 = "32893" - function returns 32893 - length 2 - substr 1,1: 4 - substr 0,2: 32893 - substring 0,1: null - substring 1,2: 4

If I was smarter, this would tell me something. Thanks again for any insight.

Share this post

Link to post
Share on other sites
Figured it out.

[code]$coded = preg_replace("!(http://)?(www.)?exam(ple)?.com/gallery/image/\?id=([0-9]+)!e", "make_slide('\\4')", $coded);[/code]

This was using the "e" modifier in preg_replace. I have no idea what its eregi equivalent is, if there is one. Thanks for looking.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.