strrpos

Welcome to our newly overhauled Zephyr pages! See our new Overview, Examples, and Index.
Feedback is welcome at bottom of each page.

Description

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

 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 necessary sub-string, which is everything after the “@” sign and beyond.

 

Converting Price (with a Decimal) from String to Number

Use Case: You have a field in a data feed you maintain with a price, but price is stored as a string and not a number. You need to do some mathematical operations, so it’s integral that the value is a number. The value has a decimal place. You’ll use map(), int(), strpos(), strrpos(), and substr() to successfully convert this value from a string to a number.

Zephyr:

In the Setup:

{content = map(content, lambda c: c + { "newMembershipPrice": int(substr(c.vars.membership_price,0,strpos(c.vars.membership_price,"."))) + int(substr(c.vars.price,strrpos(c.vars.membership_price,".")+1))/100 })}

In the Code:

{if profile.vars.member == true}
 As a member, here are your savings on each item!
{foreach content as c}
 {if length(c.vars.membership_price) > 0}
 ${number(c.price/100 - c.newMembershipPrice,2)} on {c.title}!
 {/if}
{/foreach}
{/if}

Output:

As a member, here are your savings on each item!

$2.01 on To Kill a Mockingbird!

$1.84 on Salem’s Lot!

$124.25 on Women’s Leather Jacket!

$24.50 on Men’s Chelsea Boots!

Explanation:

This script uses the map() function to add a new parameter to a content object, in this instead a parameter called “newMembershipPrice,” which will be the numerical value of a price point currently being stored as a string. In order to determine the value for this parameter, the substr() function then takes a substring of the “membership_price” custom variable, using the strpos() function to start at the beginning of the string and finding the numerical string position of the decimal point as the endpoint. For instance, using the first item in the eComm feed as an example, “12.99” would become “12”. In order to convert this to a number, the int() function is used on that substring.

Since int() drops everything after a decimal place, the value of the cents is isolated in a similar fashion. Using substr() to isolate the value, strrpos() looks at the the same “membership_price” custom variable, finding the numerical position of the decimal point, and then adding one, isolating the “cents.” Like the first part of this script, int() is then used to convert this value to a number. In order to re-convert this number into to cents, it is the divided by 100, and going off the same example, would result in “.99″. The two numberical substrings are added together, producing “12.99”, now as a number instead of a string.

In order to leverage this new value in the Code, an “if” statement is used to check for a custom field called “member” (this can be anything of your choosing, however). If “member” equals “true”, then display the savings each user will receive as a member. Next, a foreach statement loops through each piece of content, and an “if” statement checks to see if the item has a “membership_price” field associated. If so (i.e., there’s a discounted price), the item’s price is divided by 100 as to convert it to a dollars/cents format and then the newMembershipPrice value is subtracted. The number() function is then used to convert this value to a standardized format, such as “2.01”.

Top