strrpos

Description

Finds the numeric position of the last instance of a character or string within another string.

Copy
 integer strrpos( string haystack, string needle, integer offset )

Finds the numeric position of the last instance of needle in the haystack string.

  • haystack is the string to search through.
  • needle is the substring to match in haystack.
  • offset is the index (starting at 0) of the string to start at.

Example

{strrpos('Sailthru and Zephyr are fun and games','and')}   =  28
{strrpos('Sailthru and Zephyr are fun and games','and',20)}   =  9

Remove Query Parameters from URLs in a Feed

Use Case: You have URLs in a data feed that have query string parameters appended that need to be removed in order to make use of the Sailthru auto-append parameters instead. Used in conjunction with either strrpos() or strpos().

Zephyr: In the Setup:

{foreach content as c}
{if contains(c.url, "?")}
{c.url = substr(c.url,0,strrpos(c.url,"?"))}
{c.url}<br/>
{/foreach}

Output: Since the first items in both the “Media” and eComm feeds have a query parameter,

http://example.com/media/stephen-king-new-book/?utm_source=web

becomes

http://example.com/media/stephen-king-new-book/

And

http://example.com/fiction/tokillamockingbird/?utm_medium=site

becomes

http://example.com/fiction/tokillamockingbird/


Explanation: This script uses a loop to check every piece of content in the feed. Within the loop, there’s a conditional “if” statement, and if a given URL contains a “?” (i.e., it has query parameter), the URL value gets reassigned. Using substr(), we take a sub-string of the URL, starting at the beginning of the string (i.e. position 0), and ending at the position before the “?”. Since the substr() functions need a numerical “end point,” using strrpos() (aka, string reverse position), we’re able to find the numerical placement of the “?”. This allows us to retrieve the necessary sub-string, which is everything from the beginning of the URL up until before the “?”.

Create a Local Variable Based on Email Address

Use Case: You want to create a local variable of each user’s email domain to pull into. You see that you can do this with a combination of subtr() and strrpos().

Zephyr: Setup:

{domainName = substr(email,strrpos(email,"@")+1)}

HTML:

<p>Hi there! What are your experiences using {domainName}?</p>

 

Output:

Email = email@example.com

Hi there! What are your experiences using example.com?

Explanation: This script uses the subtr() function to isolate a user’s email domain for display in a template. Using substr(), we take a sub-string of the email, starting at the position after the “@”. Since the substr() functions need a numerical “end point,” strrpos() (aka, string reverse position) is used to find the numerical placement of the “@”, and using the mathematical “+” operator, add one the the value of the position. This allows us to retrieve the