Page 1 of 1

preg_replace in object context

Posted: Fri Jul 13, 2007 7:41 am
by mattcooper
Hi all,

I am trying to create a system in a CMS that will allow a user to assign available modules to positions in a template, like Joomla. Positions in the template will be marked like <% this %>. The following class has a couple of methods that are supposed to find these markers and load the inner content into an array. I've had loads of success using variations of these functions in procedural work, but can't get them to work in object context.

Procedural version is:

Code: Select all

function find_objects($data) {
			
		# hunt for navigation objects
		$object_arr = array();
		# find <% ASP-style %> tags
		preg_replace("/<\%[[]](.*)[[]]\%>/isUe", "get_object_name('\\1')", $data);
				
	}
	
	function get_object_name($name) {
					
		# companion fuction to "find_objects()" 
		global $object_arr;
		$object_arr[] = $name;
		return true;
	
	}
Full class is:

Code: Select all

class templateScan {
	
		# @class 	templateScan
		# @purpose	finds marker tags and allows the user to make associations with existing modules - like Joomla, but simpler.
		# @version	0.1
		# @date		13-07-2007
		# @author	Matt Cooper
		
		# -- BEGIN VARS ---------------------
		
		# @var		moduleDataSource
		# @purpose	define where the data for available positions is
		# @values	file/db
		# @type		string
		var $moduleDataSource;
		# @var		moduleDataFile
		# @purpose	if $positionSource = file, the location of the file
		# @value	http location from root folder
		# @type		string
		var $moduleDataFile;
		
		# -- END VARS -----------------------
		
		function getTemplateData() {
			
			# @access	private
			# @purpose	get template data
			# @return	string/html
			
			$sql = "SELECT templateName FROM templates WHERE active = '1'";
			$res = mysql_query($sql) or die('Template file error: '. mysql_error());
			while($row = mysql_fetch_assoc($res)) {			

				$data = file_get_contents($_SERVER['DOCUMENT_ROOT'] .'/templates/'. $row['templateName'] .'/theme.html');
			}
			return $data;
		}
		
		function findMarkers() {
		
			# @access	private
			# @purpose	find position markers in a template
			# @return	array
			
			preg_replace("/<\%[[]](.*)[[]]\%>/isUe", "$this->markerArray(\\1)", $this->getTemplateData());
		}
		
		function markerArray($marker) {
			
			# @access	private
			# @purpose	load markers into an array
			# @param	marker: name of marker found
			# @return	array
			
			global $markerArr; # <- may be unneeded
			$markerArr[] = $marker;
			echo '>>'. $marker;
			return $markerArr;
		}
		
		function moduleArray() {
		
			# @access	private
			# @purpose	get available postions from a file or database
			# @return	array
			
			$posArray = array();
			
			switch($this->moduleDataSource) {
			
				case 'file' :	# open the source file and extract data <- this case is for dev only
				
					$data = file_get_contents($_SERVER['DOCUMENT_ROOT'] .'/'. $this->moduleDataFile);
					# $data will be a CSV of modules available that we now have to split into an array
					$moduleArray = explode(',', $data);
				
				break;
				default 	:	# default to the database as location of data
				
					$data = ''; # do this later
				
				break;
			} # endswitch			
			return $moduleArray;
		}
		
		function returnArrays() {
		
			# @access	public
			# @purpose	return the two arrays - positions and markers
			# @return	array
			
			$this->getTemplateData();
			$this->findMarkers();
			return array('markers' => $this->markerArray(), 'modules' => $this->moduleArray());
		}
	}
The offending methods are "findMarkers()" and "markerArray()". I know that it's down to the preg_replace, but need a bit of help to figure out the solution (or, the solution itself :) !)

current error message is:
Warning: Missing argument 1 for markerarray() in /home/sageinte/public_html/sageFrame/lib/templateScan.php on line 50
All help appreciated, thanks in advance.

Posted: Fri Jul 13, 2007 8:51 am
by vigge89
Try escaping the dollar sign in the preg_replace call:

Code: Select all

preg_replace("/<\%[[]](.*)[[]]\%>/isUe", "\$this->markerArray(\\1)", $this->getTemplateData());

Posted: Fri Jul 13, 2007 8:58 am
by mattcooper
Spot on! Annoying that this behaviour changes in object context, but I've learned something... thanks for that :)