Jump to content

preg_match on a flag replacement function


Greywacke
 Share

Recommended Posts

hi there,i am currently developing a function to be inserted into the header to replace flags in templates (which are defined by %FLAGNAME optional parameter% anywhere, any amount of times, in any string passed. sofar, testing - i have the following PHP:

<?phpif (!headers_sent()) header("Content-Type: text/plain",true);function setflags($arr) {	$ret = array();	foreach ($arr as $r) {		$r = str_replace("%","",$r);		// use to split by first space if found, to get flag values		$r = preg_split("/ /",$r,2);		$a = $r[0];		$b = $r[1];		switch ($r) {			case "FIRST":				array_push($ret,"Hi ".$;				break;			case "SECOND":				array_push($ret,"Welcome");				break;			case "THIRD":				array_push($ret,"To ".$;				break;			case "FOURTH":				array_push($ret,"Hope you enjoy!");				break;			case "FIFTH":				array_push($ret,"The QuoteMe.ZA.NET Team.");				break;		}	}	return $ret;}function populateflags($str) {	// get array of flags used in string to parse (parse entire template contents' as string) and loop through array	preg_match("/%[^%]+%/",$str,$matches);	// get the values to populate with	$replace = setflags($matches[0]);	// populate flags used by doing a switch flagname procedure, recursively calling populateflags(flag) before returning	return str_replace($matches[0],$replace,$str);}$string = "%FIRST Pierre du Toit%,%SECOND% - %THIRD a complete nexus regarding consumer quoting on multiple products.%%FOURTH%Love, Light and Life - Namaste.%FIFTH%";echo populateflags($string);?>

now atm this returns the following page...

<br /><b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/dwtphovu/public_html/intellisource.co.za/test_8347379386/test.php</b> on line <b>5</b><br />Array,%SECOND% - %THIRD a complete nexus regarding consumer quoting on multiple products.%%FOURTH%Love, Light and Life - Namaste.%FIFTH%

from what i have read on PHP.net and what i understand, matches[0] should contain an array of the matches retrieved by the pcre "/%[^%]+%/".ie. $FIRST blah blah%. %SECOND%, and so forth. i am battling to focus here, could someone please assist?basically how would i retrieve an array of matches only, to parse in the function setflags.i have looked into preg_match_all aswell, but the arrays returned are "all over the place" from what i could see. ie multidimentional, with no particular order. i need a single dimentional array of items matched.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

i'm thinking now, that perhaps i should rather be using preg_replace_callback instead. :) got some research to do in the meantime... :)edit:it seems i would have to use the function with the switch functionality within the callback function. but i cannot seem to derive how exactly, this would be done.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

cool ^^this seems to work:

<?phpif (!headers_sent()) header("Content-Type: text/plain",true);function getflags($input) {	foreach ($input as $in) {		// use to split by first space if found, to get flag values		$in = preg_split("/ /",$in,2);		// build and return the values to replace with		switch ($in[0]) {			case "FIRST":				return "Hi ".$in[1];				break;			case "SECOND":				return "Welcome";				break;			case "THIRD":				return "To ".$in[1];				break;			case "FOURTH":				return "Hope you enjoy!";				break;			case "FIFTH":				return "The QuoteMe.ZA.NET Team.";				break;		}	}}function populateflags($str) {	// replace flags used in string (parse entire template contents' as string), populating flags individually and sometimes recursively in callback function.	return preg_replace_callback("/%([^%]+)%/",getflags,$str);}$string = "%FIRST Pierre du Toit%,%SECOND% - %THIRD a complete nexus regarding consumer quoting on multiple products.%%FOURTH%Love, Light and Life - Namaste.%FIFTH%";echo populateflags($string);?>

writing the following:

Hi Pierre du Toit,Welcome - To a complete nexus regarding consumer quoting on multiple products.Hope you enjoy!Love, Light and Life - Namaste.The QuoteMe.ZA.NET Team.

as the first successful test :)now i need to populate from the flag values set in the mysql database, in the callback function 0o.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

okay,i have now started implementing the function into the global database and utf8 include over the site. the functions look as follows:

function getflags($input) {	// EXAMPLE TO POPULATE WITH REAL FLAGS	foreach ($input as $in) {		// use to split by first space if found, to get flag values		$in = preg_split("/ /",$in,2);		// build and return the values to replace with		switch ($in[0]) {			// attribute flags			case "ATTRIBUTE":				return $GLOBALS["attribsarr"][$in[1]];				break;			// consumer flags			case "CONSUMERNAME":				return $GLOBALS["consumerfullname"];				break;			case "CONSUMEREMAIL":				return $GLOBALS["consumeremail"];				break;			case "CONSUMERCELL":				return $GLOBALS["consumercell"];			case "CONSUMERCITY":				return $GLOBALS["city_town"];				break;			// supplier flags			case "SUPPLIERNAME":				return $GLOBALS["recipient"][1];				break;			case "CONTACTNAME":				return $GLOBALS["recipient"][0];				break;			case "ACCMGR":				return $GLOBALS["recipient"][11];				break;			case "ACCMGRMAIL":				return $GLOBALS["recipient"][12];				break;			case "PAYGBALANCE":				return $GLOBALS["recipient"][3];			case "INVOICEAMOUNT":				return $GLOBALS["recipient"][13];				break;			// lead flags			case "LEADREFERENCE":				return "#".($GLOBALS["leadid"] + 11001000);				break;			case "LEADCATEGORY":				return $GLOBALS["cat"];				break;			case "PAYGCOST":				return $GLOBALS["servicecost"];				break;			case "DUPLICATES":				return $GLOBALS["append"];				break;			// other flags			case "SERVICENAME":				return $GLOBALS["servicename"];				break;			case "REGIONNAME":				return $GLOBALS["regionname"];				break;			// cronjob flags			case "FROMDATE":				return $GLOBALS["f"];				break;			case "TODATE":				return $GLOBALS["t"];				break;			// configurable text flags			case "CONFIGTEXT":				$sql = "SELECT * FROM 18_configurabletexts WHERE 18_configurabletexts.text_TextDescription = \"".$in[1]."\" AND 18_configurabletexts.bigint_ServiceID = ".$GLOBALS["service"].";";				$result = mysql_query_errors($sql, $conn, __FILE__, __LINE__, false);				if ($result) {					if ($row = mysql_fetch_array($result)) {						return $row["text_TextFullContent"];					}				}		}	}}function populateflags($str) {	// replace flags used in string (parse entire template contents' as string), populating flags individually and sometimes recursively in callback function.	while (preg_match("/%([^%]+)%/",$str)) {		$str = preg_replace_callback("/%([^%]+)%/",getflags,$str);	}	return $str;}

now it's to implement the populateflags("%CONFIGTEXT Configurable|Text|Name%") function all over the site, where the attribute specifies the configurable text to call from the database, this is unique to each service. i believe this "ïssue" has now been resolved - yay for the preg functions! :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...