substr

Description

Returns a portion of a string.

Copy
substr( string input , integer start [, integer length ] )

Given a string, returns a substring of that string.

  • start is the index (starting at 0) of the string to start at.
  • length is length of the substring to return.
  • If length is not passed, the remainder of the string will be returned.

Example

 {substr('Sailthru and Zephyr are fun', 13)}   = Zephyr are fun {substr('Sailthru and Zephyr are fun', 0, 8)} = Sailthru

Other Uses

substr() is not limited to taking a single string; you can input entire paragraphs to be returned as the substring. An example of this would be using the strip_tags() function:

{substr(strip_tags(content[0].content), x, y)}

In this example, we use strip_tags() to strip the HTML tags from the content in the data feed, returning a string. Afterwards, the substr() function returns a substring of the original string starting at integer index x, and ending at integer index y (counted in number of characters).

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.