Hey Everyone, I'm having problems with a little bit of php. Basically, i'm using it to search a blob of text for <sup>(digit)</sup> < with digit being any single digit number for the moment. $arrayfoot = explode("<break>", $chunk1); function confirmer($val) { $value2 = "(( $value ))"; $value3 = "<sup>.$val.</sup>"; if($val == $key) { return $value2; } else { return $value3; } } foreach($arrayfoot as $key=>$value) { preg_replace('{<sup>([0-9]{1})</sup>}', confirmer($1), $text); } Code (markup): My code looks like that. Now my problem at the moment is that it's no longer hitting the pieces of code it was meant to, it's stopped catching them alltogether. Outside of the foreach loop it worked, but I couldn't get the preg_replace to echo $function[number]; , it just refused too. I'm willing to give some money to whoever can help me, I've been wrangling with it for near as makes no difference three hours now. It was meant to be a 'little' job. Fun stuff!
You have no variable called $1... If you are trying to pass the set created in the first part of your regexp to the function, preg_replace doesn't work that way. You cannot call a function in the second half unless you have the value you are passing already. That's why it's passed as '$1' as a fixed string value, not as a variable. Second, you aren't doing anything with the returned value from your preg_replace. Preg_replace does NOT change the value of the string passed to it, it RETURNS the change you make... and your expression is invalid. You also are not passing $key to your function, and are not declaring it accessable as a global... and there is no $value variable declared for that function to do anything with. From that I really can't even determine exactly what it is you are even trying to do. Without knowing what $text contains, what $chunk1 is, it's pretty much meaningless especially with all the variables that "don't exist" in your snippet.
What are you trying to replace <sub>(digit)</sub> with? and under which circumstances? It seems some of the variables are no longer correct with the rearrangement. Just a bit more information (pseudocode of what you want to do is fine) and it should be simple.
Is this close ? I imagine you notice a few extras I added so I could see what i was doing. <?php $text = '<sup>0</sup><sup>1</sup><sup>2</sup>'; $chunk1 = 'zero<break>one<break>two'; $arrayfoot = explode("<break>", $chunk1); $text = preg_replace_callback('#<sup>(\d)</sup>#i', create_function( '$matches', 'global $arrayfoot; return isset($arrayfoot[$matches[1]]) ? "(({$arrayfoot[$matches[1]]}))" : $matches[0];' ), $text); echo htmlentities($text); ?> PHP: