jaymc Posted December 19, 2008 Share Posted December 19, 2008 I have this class class BBParse { public static function maxsize($call, $size) { echo $size; if ($call == "size" && $size > "4") {$size = "4";} return $size; } } And this preg_replace $string = preg_replace('/\['.$tag.'=(.+?)\](.+?)\[\/'.$tag.'\]/is','<'.$data[0].' '.$data[1].'='.BBParse::maxsize($data['1'], '$1').'>$2</'.$data[0].'>',$string); This part of the preg_replace is the issue BBParse::maxsize($data['1'], '$1') As you can see I am trying to manipulate the value of $data['1'] and $1. The $data['1'] is seen by maxsize() no problem, but $1 is seen by maxsize() as $1, as in that is the value when in actual fact the value of $1 is always 1,2,3 or 4 Its doing that because I have it wrapped in appostrophies '$1' so it sees it as a string, but if I take them off, the script dies Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/ Share on other sites More sharing options...
genericnumber1 Posted December 19, 2008 Share Posted December 19, 2008 If you wrap something in single quotes it uses it as a string literal without parsing its content save for single quote escapes. <?php $test = 'value'; echo 'test: $test'; // "test: $test" echo "test: $test"; // "test: value" ?> and a variable can only start with a letter or an underscore, so $1 is an invalid variable name. Edit: And please don't wrap all of your literals, including ints, in quotes. It's inefficient as php will have to cast them back before using them as the types for which you wish to use them... plus it's anoying to read. use $variable, not "$variable". 7, not "7" or '7'. Sorry, pet peeve I've been seeing WAY too much lately. Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719534 Share on other sites More sharing options...
jaymc Posted December 19, 2008 Author Share Posted December 19, 2008 and a variable can only start with a letter or an underscore, so $1 is an invalid variable name. Hmm, preg_replace returns it as $1 by default, how can I get around that? Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719536 Share on other sites More sharing options...
genericnumber1 Posted December 19, 2008 Share Posted December 19, 2008 ... preg_replace returns a string or an array and has nothing to do with how it's named. If you're talking about the $1 in the replacement, that's not a variable. Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719538 Share on other sites More sharing options...
genericnumber1 Posted December 19, 2008 Share Posted December 19, 2008 Ah, I think I see what you're doing, but you can't do it in that manner, you will have to split it up into different function calls as the preg_replace routine can't pass replacements into functions. Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719540 Share on other sites More sharing options...
Adam Posted December 19, 2008 Share Posted December 19, 2008 genericnumber1: In preg_replace() $1, $2, etc. are valid variables that can be used within the replacement parameter.. http://uk3.php.net/manual/en/function.preg-replace.php Also: $test = 'value'; echo 'test: $test'; ...will still produce "test: value" .. jaymc: Quite fine to use $1, $2, etc but I'm not sure you can use them to call another function from within preg_replace?? You need to remove the single quotes from the arrays though, unless you have created them that way? $data['1'] = 'foo'; // valid way to do it but.. print $data[1]; // ... won't work! print $data['1']; // .... will! $data[2] = 'bar!'; // valid way to do it but.. print $data['2']; // ... won't work! print $data[2]; // ... will! Remember 1 != '1' .. I'm not saying this will definitely work though.. A Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719543 Share on other sites More sharing options...
jaymc Posted December 19, 2008 Author Share Posted December 19, 2008 BBParse::maxsize($data['1'], '$1') How can I get maxsize to see what $1 is then? Can anyone provide a different way to go about this? Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719546 Share on other sites More sharing options...
genericnumber1 Posted December 19, 2008 Share Posted December 19, 2008 $1, etc aren't variables, they're replacement commands... similar to %1, etc. I certainly wouldn't consider them variables. Also: $test = 'value'; echo 'test: $test'; ...will still produce "test: value" .. and no, it won't. Try it. http://www.google.com/search?q=difference+between+single+and+double+quotes+in+php Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719548 Share on other sites More sharing options...
Adam Posted December 19, 2008 Share Posted December 19, 2008 Hah boy do I feel foolish! Though I would still consider them variables being as they represent...variable data? Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719552 Share on other sites More sharing options...
genericnumber1 Posted December 19, 2008 Share Posted December 19, 2008 Hah boy do I feel foolish! Though I would still consider them variables being as they represent...variable data? Perhaps they could be referred to as variables, but since they accept things like ${1}1 I think of them more as formats than variables... I suppose it's all just semantics. Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719553 Share on other sites More sharing options...
Adam Posted December 19, 2008 Share Posted December 19, 2008 Fair enough, but then again you can have such complex syntax as: ${$a[1]} and: ${$a}[1] with just plain old variables.. Isn't ${1}1 just so that the trailing 1 will be treated as a literal? A Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719557 Share on other sites More sharing options...
genericnumber1 Posted December 19, 2008 Share Posted December 19, 2008 To be honest, I'm not quite sure, I never use the preg functions without a reference (hell, i really never use them). I'm more relating them to the printf formats in my head, which I am more familiar with. Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719558 Share on other sites More sharing options...
jaymc Posted December 19, 2008 Author Share Posted December 19, 2008 Can anyone think of a solution to my problem Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719564 Share on other sites More sharing options...
effigy Posted December 19, 2008 Share Posted December 19, 2008 Look into the /e modifier; this will let you pass $1 after it is defined by the regular expression, rather than having it evaluated beforehand. Quote Link to comment https://forums.phpfreaks.com/topic/137678-preg_replace-function/#findComment-719773 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.