Jump to content

php_replace_callback to replace in array?


Greywacke

Recommended Posts

hi there,can't seem to find much relative reference for using php_replace_callback to replace strings within an array of strings...here are the functions used and the line that is used later in the code, after the system variables are populated.

// functions usedfunction latepost($input) {	print_r($input);	foreach ($input as $in) {		return newpost($in);	}}function newpost($key) {	if (strstr($key,"\$")!==false) {		$syskey = str_replace("\$","",$key);		return $GLOBALS[$syskey];	} else {		if ($_POST[$key]=="") {			return $GLOBALS["dynarr"][$key];		} else {			return $_POST[$key];		}	}}// only later once the variables have been created, is it indended to populate them.$sysarr = preg_replace_callback("/%([^%]+)%/",latepost,$sysarr);

and here is the array that needs replacing. only two matches in the array, mind you! yet this array is returned as it is sent to be parsed, and i do not understand why...

Array(    [service_id] => 1    [ZipCode] => 99501    [Name] => Pierre du Toit    [City] => Anchorage, 99501    [Phone] => 072-915-4799    [County] => Anchorage    [Email] => pierre@canopyxchange.za.net    [state] => Alaska    [iM] => Skype: greywacke_2k7    [Make] => FORD    [Condition] => NEW    [Model] => RANGER    [Function] => Commercial Deluxe    [Year] => 1998-LATEST    [Product] => Aluminum    [CabSize] => REGULARCAB    [Color] => COLORCODED    [bedSize] => SHORTBOX    [budget] => $1,750 plus    [Comments] => testing dynamic handler - black cap.    [Fitment] => Not within next 3 months    [$duplicates] => %$duplicates%    [$space] =>      [l360firstname] => Pierre du Toit (%$duplicates%)    [$leadid] => %$leadid%    [$formname] => TruckCapXchange    [l360makemodel] => FORD RANGER    [l360other] => REGULARCAB SHORTBOX Commercial Deluxe NEW    [$hash] => #    [l360leadid] => #%$leadid%    [$obracket] => (    [$cbracket] => ))

with this logic, %$leadid% and %$duplicates% should be replaced - but do not get... would someone please be so kind as to point out or even nudge me in the direction of finding the flaw in my logic! :S

Link to comment
Share on other sites

for some reason it is not printing the $input array either, within the latepost function... :/

Link to comment
Share on other sites

okay, will atempt this... however, in the other place where preg_replace_callback is used - the function name is passed like here 0obut i do not see how this would be different just because i am parsing an array instead of a string...unfortunately it is not finding anything - either the regular expression is not as it should be, or the $ in the array string elements might be messing it up somehow:

// "late" $sysarr element population functionsfunction latepost($input) {	print_r($input);	foreach ($input as $in) {		return newpost($in);	}}function newpost($key) {	if (strstr($key,"\$")!==false) {		$syskey = str_replace("\$","",$key);		return $GLOBALS[$syskey];	} else {		if ($_POST[$key]=="") {			return $GLOBALS["dynarr"][$key];		} else {			return $_POST[$key];		}	}}// attempted late variable population$sysarr = preg_replace_callback("/%([^%]+)%/","latepost",$sysarr);// print_r'd $sysarr AFTER preg_replace_callback :/Array(    [service_id] => 1    [ZipCode] => 99501    [Name] => Pierre du Toit    [City] => Anchorage, 99501    [Phone] => 072-915-4799    [County] => Anchorage    [Email] => pierre@canopyxchange.za.net    [state] => Alaska    [iM] => Skype: greywacke_2k7    [Make] => GMC    [Condition] => NEW    [Model] => SIERRA3500    [Function] => Commercial Deluxe    [Year] => 2007-LATEST    [Product] => Aluminum    [CabSize] => CREWCAB    [Color] => COLORCODED    [bedSize] => SHORTBOX    [budget] => $1,750 plus    [Comments] => testing dynamic handler - BLACK CAP    [Fitment] => Not within next 3 months    [$append] => [b]%$append%[/b]    [$space] =>      [l360firstname] => Pierre du Toit ([b]%$append%[/b])    [$leadid] => [b]%$leadid%[/b]    [$formname] => TruckCapXchange    [l360makemodel] => GMC SIERRA3500    [l360other] => CREWCAB SHORTBOX Commercial Deluxe NEW    [$hash] => #    [l360leadid] => #[b]%$leadid%[/b]    [$obracket] => (    [$cbracket] => ))

note the sections of the array strings which were supposed to be replaced by the callbacks (demarkated in bold on the $sysarr array). the variables $leadid and $append are supposed to be replaxced by their $GLOBAL values - why do they not, despite referring to the function as a string! :S

Link to comment
Share on other sites

it gets sent an array of references to various strings to get, which it is then supposed to retrieve the value for either from the $GLOBALS array (a collection of page variables), $sysarr array (all the variable arrays) or $_POST array (posted data) by calling the newpost function, passing the individual array elements (which are variable names if detected between %'s.the matches from the search usually get passed by preg_replace_callback are usually sent as an array. i was attempting to print_r() this array of matches from within this function, but to no avail! :/preg_replace_callback runs the callback function, which gets sent an array with [0] the match and [1] the first bracket of the regular expression. one is supposed to return the replacement.

Link to comment
Share on other sites

okay, i created a test page to perform the same function, with expected results however.

<?phpfunction newpost($key) {	if (is_array($key)) {		return newpost($key[1]);	}	if (strstr($key,'$')!==false) {		$syskey = str_replace('$','',$key);		return $GLOBALS[$syskey];	} else {		if ($_POST[$key]=='') {			return $GLOBALS['dynarr'][$key];		} else {			return $_POST[$key];		}	}}$dynarr = array(		'one'=>'111',		'two'=>'222',		'three'=>'33333',		'four'=>'4444',		'five'=>'5555'	);$test = array(	'element one',	'element %two%',	'element %$three%',	'element %four%',	'element %$five%',	);$three = 'blah!';$five = 'bleh!!';	$test = preg_replace_callback('/%([^%]+)%/',newpost,$test);	print_r($test);?>

and here is the result of this page:

Array(	[0] => element one	[1] => element 222	[2] => element blah!	[3] => element 4444	[4] => element bleh!!)

in otherwords it works as expected! however the implemented one does not. the only difference between the two implementations are arrays of length 5 used here, and the $dynarray (instead of test) has a length of 32! in the live implementation, $dynarr is used to store variables to replace with if not a page variable, eg concatenated values. it is also the array that is replaced on. if a variable was not available at the time of the $dynarray creation - it inserts a % enclosed representation of the variable. these are what i need to replace! :/

Link to comment
Share on other sites

yes it would seem so... but why? what needs to change in order to get it to work? :S

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...