maxwelton Posted August 6, 2006 Share Posted August 6, 2006 [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;}[/code]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:[code]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); } }}[/code]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! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 6, 2006 Share Posted August 6, 2006 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 + 67will evaluate as "a string 9" + 67 which you can't do!!!! Quote Link to comment Share on other sites More sharing options...
maxwelton Posted August 7, 2006 Author Share Posted August 7, 2006 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:[code]...,make_slide("howdy\\4"),...[/code]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. Quote Link to comment Share on other sites More sharing options...
maxwelton Posted August 7, 2006 Author Share Posted August 7, 2006 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: 4If I was smarter, this would tell me something. Thanks again for any insight. Quote Link to comment Share on other sites More sharing options...
maxwelton Posted August 7, 2006 Author Share Posted August 7, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.