Splitting a string into arrays in Smarty
Posted: August 8, 2010 Filed under: Programming | Tags: array, php, programming, smarty, split, splitting, string, variable Leave a comment »This isn’t supposed to be a blog about programming, but I’m working a lot with PHP and Smarty these days, and whenever I manage to crack a problem, I get this strange feeling of acchievement. Then I come here to brag.
Anyway, here goes:
«How do I split a string in Smarty template engine?»
Well kind Sir, that’s quite an easy task. Here’s an example:
{assign var=”teststring” value=”31|32″}
{assign var=”testsplit” value=”|”|explode:$teststring}
{$testsplit[0]}<br />
{$testsplit[1]}
Let’s for example say you have a bunch of Meta Keywords that are bundled together in the database (games,game,online,playing) – and you want to separate them, so that you can create keyword-specific landingpages for on-site search optimalization (games – game - online - playing).
Here’s how you do it:
(we’re assuming you have a variable called $MetaKeywords!)
{* Splitting a long string into a bunch of different arrays *}{assign var=”keywords” value=”,”|explode:$MetaKeywords}{foreach from=$keywords item=keyword}<a href=”/index.php?search={$keyword}”>{$keyword|trim}</a>, {/foreach}

Recent Comments