I'm given a string of HTML with some "tokens" in it. They are structured like {:TOKEN_NAME:}
For instance:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{:TITLE:}
{:DESCRIPTION:}
{:KEYWORDS:}
{:GOOGLE-VERIFY:}
{:BING-VERIFY:}
<link rel="stylesheet" href="/assets/css/base.styles.css?_=<?php echo time(); ?>" type="text/css" />
<link rel="stylesheet" href="/assets/css/custom.css?_=<?php echo time(); ?>" type="text/css" />
<!--[if lt IE 9]>
<script type="text/javascript" src="/assets/js/modernizr.min.js"></script>
<![endif]-->
</head>
<body>
<article data-role="page-wrapper" class="container-fluid">
<header class="row-fluid" data-role="page-header">
<h1 class="span5 logo pull-right"><a href="http://www.o7thwebdesign.com">o7th Web Design</a></h1>
<nav class="span7">
{:PAGE-CONTENT:}
</nav>
</header>
<section class="row-fluid" data-role="page-container">
</section>
<footer class="row-fluid" data-role="page-footer">
</footer>
</article>
<script type="text/javascript" src="/assets/js/scripts.js?_=<?php echo time(); ?>"></script>
<script type="text/javascript" src="/assets/js/custom.js?_=<?php echo time(); ?>"></script>
{:GOOGLE-UA:}
</body>
</html>
I am also given an associative array, with the token names, and what they should be replaced with, as such:
// Populate and pull all global smarttags
private function PullGlobalSmartTags($values){
return array(array('Name'=>'{:TITLE:}', 'Replacement'=>'<title>' . $values[0] . '</title>'),
array('Name'=>'{:DESCRIPTION:}', 'Replacement'=>'<meta name="description" content="' . $values[1] . '" />'),
array('Name'=>'{:KEYWORDS:}', 'Replacement'=>($values[22]) ? null : '<meta name="keywords" content="' . $values[2] . '" />'),
array('Name'=>'{:GOOGLE-UA:}', 'Replacement'=>'<script type="text/javascript">var _gaq = _gaq || [];_gaq.push([\'_setAccount\', \'' . $values[3] . '\']);_gaq.push([\'_trackPageview\']);(function(){var ga = document.createElement(\'script\');ga.type = \'text/javascript\'; ga.async = true;ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);})();</script>'),
array('Name'=>'{:GOOGLE-VERIFY:}', 'Replacement'=>'<meta name="google-site-verification" content="' . $values[4] . '" />'),
array('Name'=>'{:BING-VERIFY:}', 'Replacement'=>'<meta name="msvalidate.01" content="' . $values[5] . '" />'),
array('Name'=>'{:PAGE-CONTENT:}', 'Replacement'=>$values[6]),);
}
Please assume all values in $values
populate, and populate correctly (because they do...)
I know with str_replace, and preg_replace I can simply pass arrays in as my needle, replacement, and haystack, however everything I am seeing only shows non-associative arrays.
My question is, how can I do these replacements? I know that I could simply loop through the array, and do my replacements one at a time, but is there a way to do this without looping?
This does do the trick:
for($i=0; $i<$gsCt; ++$i){
$rettemp = str_replace($GlobalSmartTags[$i]['Name'], $GlobalSmartTags[$i]['Replacement'], $rettemp);
}
however, I do not believe it is the most efficient method to do this.