Jump to content


Photo

eregi_replace into function?


  • Please log in to reply
4 replies to this topic

#1 maxwelton

maxwelton
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 06 August 2006 - 09:43 PM

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

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:

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:

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

You'd get

35000 length: 2 value: 0

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!

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 06 August 2006 - 10:01 PM

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!!!!
follow me on twitter @PHPsycho

#3 maxwelton

maxwelton
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 07 August 2006 - 12:20 AM

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:

...,make_slide("howdy\\4"),...

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

howdy790 length 7

And for "14258"?

howdy14258 length 7

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.

#4 maxwelton

maxwelton
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 07 August 2006 - 12:45 AM

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.

#5 maxwelton

maxwelton
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 07 August 2006 - 02:10 AM

Figured it out.

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

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users