Location: PHPKode > scripts > Price Comparison Shopping by DataFeedFile > price-comparison-shopping-by-datafeedfile/include/DFF_dffml.func.php
<?php
/**************************************************************
*
* DataFeedFile.com (DFF) Framework API
* Copyright (c) 2008 DataFeedFile.com
*
* DFF Framework API uses CreativeCommons.org by-sa license.
* This license lets others remix, tweak, and build upon your work even for commercial reasons,
* as long as they CREDIT DataFeedFile.com and license their new creations under the identical terms.
* This license is often compared to open source software licenses.
* All new works based on yours will carry the same license, so any derivatives will also allow commercial use.
* Please read our license.txt file for more detail.
*
**************************************************************/
// GENERAL SYNTAX:  <|DFF| <<<func( param~param )>>> ^ fieldname=>subarray=>subarray | param~param~param |>
// fieldname can be regular or PARENT{...} or PHP{...} or PHPEVAL{...}
// param(s) are in the format of key=value pair
// functions: see list of functions here: http://wiki.datafeedfile.com//wikka.php?wakka=DFFDFFMLFunctions

function DFF_merge_array_DFFML($DFFML_string, $data, $data_parent=FALSE, $show_error=FALSE){

	$output = '';
	$pos1 = 0; // beginning of <|DFF|
	$pos2 = 0; // end of <|DFF|
	$pos3 = 0; // beginning of |>
	$pos4 = 0; // end of |>
	$pos_fieldname_end = 0;
	$params = array();
	$pos_last_copied = 0;
	$done = FALSE;
	$toggle = array(NULL,NULL,NULL);
	
	if($data_parent===FALSE) $data_parent_work = &$data;
		else $data_parent_work = &$data_parent;
	
	$len_total = strlen($DFFML_string);
		
	do{
		$field_name = FALSE;
		$field_name2 = FALSE;
		$field_name3 = FALSE;
		$function_name = FALSE;
		$function_params = FALSE;
		$field_override = FALSE;
		
		$pos1 = strpos($DFFML_string,'<|DFF|',$pos_last_copied);
		
		if($pos1!==FALSE){
			
			$pos2 = $pos1+5;
			
			//GET FIELDNAME
			$pos_fieldname_end = strpos($DFFML_string,'|',$pos2+1);
			if($pos_fieldname_end!==FALSE){
				$field_name = substr($DFFML_string, $pos2+1, $pos_fieldname_end-$pos2-1);
			}
			//echo "field_name: ".$field_name."<br>";
			
			//CHECK IF THE FIELDNAME HAS FUNCTION
			if(strpos($field_name,'^')!==FALSE){
				//HAS FUNCTION
				$fn_arr = explode('^',$field_name);
				$function_name = $fn_arr[0];
				$field_name = $fn_arr[1];
				//PARSE FUNCTION NAME PARAMETERS
				$fn_pos1 = strpos($function_name,'(');
				if($fn_pos1!==FALSE){
					$fn_pos2 = strpos($function_name,')',1);
					if($fn_pos2!==FALSE){
						$fn_params = trim(substr($function_name,$fn_pos1+1,$fn_pos2-$fn_pos1-1));
						if($fn_params!=''){
							$function_params = explode('~',$fn_params);
							//exit(print_r($function_params,TRUE));
						}
						//echo "function_params = <pre>".print_r($function_params,TRUE)."</pre><br>";
						$function_name = substr($function_name,0,$fn_pos1);
					}
				}
			}

			//CHECK IF FIELDNAME POINT TO PARENT
			if(($parent_pos1 = strpos($field_name,'PARENT{'))!==FALSE){
				$parent_pos1 += 7;
				$field_override = 'PARENT';
				if(($parent_pos2 = strpos($field_name,'}|',$parent_pos1))!==FALSE){
					$field_name = substr($field_name,$parent_pos1,$parent_pos2-$parent_pos1);
				} else {
					//ERROR
					if($show_error) echo '<br>[DFFERR]DFFML: PARENT{} syntax error near '.$field_name.'<br>';
				}
			}

			//CHECK IF FIELDNAME POINT TO PHP GLOBAL
			if(($php_pos1 = strpos($field_name,'PHP{'))!==FALSE){
				$php_pos1 += 4;
				$field_override = 'PHP';
				if(($php_pos2 = strpos($field_name,'}|',$php_pos1))!==FALSE){
					$field_name = substr($field_name,$php_pos1,$php_pos2-$php_pos1);
				} else {
					//ERROR
					if($show_error) echo '<br>[DFFERR]DFFML: PHP{} syntax error near '.$field_name.'<br>';
				}
			}

			//CHECK IF FIELDNAME POINT TO PHPEVAL
			if(($phpe_pos1 = strpos($field_name,'PHPEVAL{'))!==FALSE){
				$phpe_pos1 += 8;
				$field_override = 'PHPEVAL';
				if(($phpe_pos2 = strpos($field_name,'}|',$phpe_pos1))!==FALSE){
					$field_name = substr($field_name,$phpe_pos1,$phpe_pos2-$phpe_pos1);
				} else {
					//ERROR
					if($show_error) echo '<br>[DFFERR]DFFML: PHP{} syntax error near '.$field_name.'<br>';
				}
			}


			//CHECK IF FIELDNAME IS ARRAY WITH SUB-ARRAY
			if(strpos($field_name,'=>')!==FALSE){
				//HAS SUB-ARRAY
				$fn_arr = split('=>',$field_name);
				if(count($fn_arr)==2){
					$field_name = $fn_arr[0];
					$field_name2 = $fn_arr[1];
				}
				if(count($fn_arr)==3){
					$field_name = $fn_arr[0];
					$field_name2 = $fn_arr[1];
					$field_name2 = $fn_arr[2];
				}
			}

			
				//FOUND PLACEHOLDER
				$params = array();
				$pos3 = strpos($DFFML_string,'|>',$pos2+1);
				if($pos3!==FALSE){
					$pos4 = $pos3+2;
					$prev_pos_last_copied = $pos_last_copied;
					$pos_last_copied = $pos4;
					//CHECK OF PARAM EXISTS
					if($pos_fieldname_end<$pos3){
						//HAS PARAM(S)
						$len_param = ($pos3-$pos_fieldname_end)-1;
						//echo "Param Length: ".$len_param."<br>";
						$param_all = substr($DFFML_string,$pos_fieldname_end+1,$len_param);
						//echo "param_all = ".$param_all."<br>";
						$param_split = explode('~',$param_all);
						//echo "param_split = <pre>".print_r($param_split,TRUE)."</pre><br>";
						foreach($param_split as $ps){
							$temp_arr = split('=',$ps);
							$params[$temp_arr[0]] = $temp_arr[1];
						}
						//echo "params = <pre>".print_r($params,TRUE)."</pre><br>";
					}

					//ADD PREVIOUS STRING BEFORE PLACE HOLDER
					//echo "prev_pos_last_copied = ".$prev_pos_last_copied."<br>";
					$prev_content = substr($DFFML_string,$prev_pos_last_copied,$pos1-$prev_pos_last_copied);
					//echo "prev_content = <pre>".$prev_content."</pre>";
					$output .= $prev_content;
						
					//CHECK IF FIELD IS ROWS
					if(substr($field_name,0,5)=='ROWS='){
						//// ROW ////
						$sub_array_name = str_replace('ROWS=','',$field_name);
						//echo "sub_array_name = ".$sub_array_name."<br>";
						// GENERATE RECURSIVE CONTENT
						$ending_row_needle = '<|DFF|ROWS='.$sub_array_name.'|END|>';
						//echo "ending_row_needle = ".$ending_row_needle."<br>";
						$pos1_ending_row_needle = strpos($DFFML_string,$ending_row_needle,$pos4);
						//echo "pos1_ending_row_needle = ".$pos1_ending_row_needle."<br>";
						if($pos1_ending_row_needle!==FALSE){
							$recursive_content = substr($DFFML_string,$pos4,$pos1_ending_row_needle-$pos4);
							//echo "Recursive content: [[ ".$recursive_content." ]]";
						} else {
							$recursive_content = FALSE;
						}
						
						//echo "Goes to recursive...<br>";
						if($recursive_content!==FALSE){
							//SET OFFSET
							if($params['offset']>1) $offset=$params['offset']; else $offset=1;
							//SET LIMIT
							if($params['limit']>0) $limit=$offset+$params['limit']; else $limit=9999999;
							//for($i=$offset; $i<$limit; $i++){
							$i=$offset;
							foreach($data[$sub_array_name] as $row_data){
								if($i>=$offset && $i<$limit){
									//$output_recursive = template_layout_merger($recursive_content,$data[$sub_array_name][$i],&$data_parent_work,$show_error);
									$output_recursive = DFF_merge_array_DFFML($recursive_content,$row_data,&$data_parent_work,$show_error);
									$output .= $output_recursive;
								}
								$i++;
							}
						}
						
						$pos2_ending_row_needle = $pos1_ending_row_needle + strlen($ending_row_needle);
						$prev_pos_last_copied = $pos_last_copied;
						$pos_last_copied = $pos2_ending_row_needle;
						
					} else {
						//// NOT ROWS ////
						// COMPOSE OUTPUT - DO FIELDNAME TO VARIABLE SUBSTITUTION //
						//$output_data = FALSE;
						if($field_override=='PARENT') $output_var_str = 'data_parent_work';
						elseif($field_override=='PHP') $output_var_str = 'GLOBALS';
						else $output_var_str = 'data';
						
						if($field_override=='PHP'){
							// USE PHP GLOBAL VARS
							if($field_name3!==FALSE) {
									eval('$output_data = $GLOBALS[\''.$field_name.'\'][\''.$field_name2.'\'][\''.$field_name3.'\'];');
							} elseif($field_name2!==FALSE){
									eval('$output_data = $GLOBALS[\''.$field_name.'\'][\''.$field_name2.'\'];');
							} elseif($field_name!==FALSE){
									eval('$output_data = $GLOBALS[\''.$field_name.'\'];');
							}
						} elseif($field_override=='PHPEVAL') {
							$output_data = eval(DFF_merge_array_DFFML($field_name,$data));
							if($output_data===FALSE && $show_error==TRUE){
								 echo '<br>[DFFERR]DFFML: PHPEVAL{} syntax error near '.$field_name.'<br>';
							}
						} else {
							// NOT PHP GLOBAL
							if($field_name3!==FALSE) {
								if( isset(${$output_var_str}[$field_name][$field_name2][$field_name3]) ){
									//$output_data = $data[$field_name][$field_name2][$field_name3];
									//$output_var_str .= '['.$field_name.']['.$field_name2.']['.$field_name3.']';
									$output_data = ${$output_var_str}[$field_name][$field_name2][$field_name3];
								}
							} elseif($field_name2!==FALSE){
								if( isset(${$output_var_str}[$field_name][$field_name2]) ){
									//$output_data = $data[$field_name][$field_name2];
									//$output_var_str .= '['.$field_name.']['.$field_name2.']';
									$output_data = ${$output_var_str}[$field_name][$field_name2];
								}
							} elseif($field_name!==FALSE){
								if( isset(${$output_var_str}[$field_name]) ){
									//$output_data = $data[$field_name];
									//$output_var_str .= '['.$field_name.']';
									$output_data = ${$output_var_str}[$field_name];
								}
							}
						}
						//echo "outputvarstr: ".$output_var_str."<br>";
						//$output_data = ${$output_var_str};
						//echo "outputdata: ".$output_data."<br>";
						
						// PERFORM SUBSTITUTION
						if($output_data!==FALSE){
							// CHECK IF NEED TO PASS-THROUGH FUNCTION
							if($function_name!==FALSE){
								//exit($function_name);
								switch($function_name){
									case 'urlencode':
									case 'urldecode':
									case 'ucfirst':
									case 'lcfirst':
									case 'ucwords':
									case 'strtolower':
									case 'strtoupper':
									case 'strip_tags':
									case 'trim':
									case 'ltrim':
									case 'rtrim':
									case 'md5':
									case 'strlen':
									case 'htmlentities':
									case 'html_entity_decode':
									case 'htmlspecialchars':
									case 'htmlspecialchars_decode':
									case 'stripslashes':
									case 'addslashes':
									case 'stripcslashes':
									case 'addcslashes':
									case 'quotemeta':
									case 'count':
									case 'array_sum':
													 eval('$output_data = '.$function_name.'($output_data);');
													 break;
									case 'date':
													 if($function_params[0]=='') $function_params[0] = 'M j Y';
													 eval('$output_data = date(\''.$function_params[0].'\',strtotime($output_data));');
													 break;
									case 'number_format':
													 if($function_params[0]=='') $function_params[0] = 2;
													 if($function_params[1]=='') $function_params[1] = '.';
													 if($function_params[2]=='') $function_params[2] = ',';
													 eval('$output_data = number_format($output_data,'.$function_params[0].',\''.$function_params[1].'\',\''.$function_params[2].'\');');
													 break;
									case 'substr':
													 if($function_params[0]=='') $function_params[0] = 0; //start
													 eval('$output_data = substr($output_data,'.$function_params[0].(($function_params[1]!='')?','.$function_params[1]:'').');');
													 break;
									case 'str_replace':
													 eval('$output_data = str_replace(\''.$function_params[0].'\',\''.$function_params[1].'\',$output_data);');
													 break;
									case 'rand':
													 $output_data = rand($function_params[0],$function_params[1]);
													 break;
									case 'toggle':
													 if($toggle[$function_params[0]]===NULL || $toggle[$function_params[0]]==$function_params[2])
													 	$toggle[$function_params[0]] = $function_params[1];
													 else
													 	$toggle[$function_params[0]] = $function_params[2];
													 $output_data = $toggle[$function_params[0]];
													 break;
													 
								}	
							} 
							$output .= $output_data;
						}

					}
					
				} else {
					$pos_last_copied = $pos2;
					continue;
				}
			
		} else $done = TRUE;
		
	}while($done == FALSE);
		
	//echo "data = <pre>".print_r($data,TRUE)."</pre><br>";
	//echo "<br>Output:<br>".$output."<br>";
	$output .= substr($DFFML_string,$pos_last_copied);
	
	return $output;
}

?>
Return current item: Price Comparison Shopping by DataFeedFile