replace
Description
Performs string replacement.
string replace( string input , string search , string replace )
Performs a search-and-replace on all occurrences of search
in input
, replacing them all with replace
.
Example
{replace('New York, New England, New Jersey', 'New', 'Old')} = Old York, Old England, Old Jersey
Replace Dashed Values from a Data Feed with Spaces
Use Case: You want to display a genre value from the “sailthru_genre” custom variable in the eCommerce feed, but the content is being spidered with a dash. For example, mysteries-and-suspense. To make this more reader-friendly, you’ll use replace() to do a find-and-replace on each dash and only if that variable exists on the item.
Zephyr:
Picked for You:
{foreach content as c}
{c.title} {if c.vars.sailthru_genre} in the category {replace(c.vars.sailthru_genre,"-"," ")} {/if}
<br/>
{/foreach}
Output: Picked for You: To Kill a Mockingbird in the category mysteries and thrillers Salem’s Lot in the category horror and suspense Women’s Leather Jacket Invisible Monsters: A Novel in the category horror and suspense Men’s Chelsea Boots From the Fashion Blog: The Most Comfortable Socks You’ll Ever Own
Explanation: This script uses the replace() function to do a find and replace on a certain value, in this instance, the “sailthru_genre” content variable. In the code, a foreach loop iterates through the content, populating the title of the item. Using an “if” conditional, a check is done to see if that piece of content has the “sailthru_genre” variable. If it does, display the blurb “in the category” along with the value of that variable. Since the feed has values such as “mysteries-and-thrillers” for that variable, the replace() function finds every instance of a hyphen (t”-“) and replaces with an blank space (” “). For example, “mysteries-and-thrillers” thus becomes “mysteries and thrillers”.