Split a string into multiple parts in a Merge Template

This article gives a step by step example of splitting up a string into various parts. This is applicable to fields such as Addresses.


If you are creating a Merge Template and need to break out certain sections of an overall string of text, you will need to break up that text into substrings. For example, let's say we are creating a Merge Template for a Contract and need to pull the "From Address" of the form into our merge template. 

You can see there is a single merge tag for the field "From Address" and it is {$projectteam_762}. 

If we add that to our Merge Template and print one of our Contracts using that template, we will get something like this:

In some cases, that works just fine, but in others we might want to break out the address on our output to show:

  • Street

  • City

  • State Zip


We are going to need to split up the string into substrings using Smarty Tags. The substring tag is written as "substr" and takes two variables:

  1. Starting position

  2. End position

The format of your substring should be the following (using 0 as start and 9 as end):


Relating this to our original merge tag, it would look like this:


If you were to run this, we would get the following result for 14291 Park Meadow Drive, Chantilly, VA 20151:

14291 Park

This obviously is not correct, because it is not returning our full "Street" but we can see our tag is working. It is starting at the first position "1" and going to the ninth position "k".  Therefore, we have the result "14291 Park".

Adding in a hard-coded "end position" like "9" isn't going to work. We need to add something more dynamic to our tag. 

String Position

Luckily, there is another Smarty Tag that we can utilize called string position (written "strpos"). In the case we are trying to return the street only, we want to return everything up to the first comma. So we need to find the position of that comma. 

This would look something like:


Relating this to our original merge tag, it would look like this:


If you were to run this, we would get the following result for 14291 Park Meadow Drive, Chantilly, VA 20151:


So now we have found the position of the first comma in our address string. 

Output the "Street"

If you put what we've learned together into one overall tag, we will get the desired result for outputting only the street from our full address. This looks like:


This tag says:

For the field related to tag $projectteam_762, return a substring starting at the first position (0) all the way until the position of the comma symbol within the full string $projectteam_762. 

Your result is:

14291 Park Meadow Drive

Output the "City"

Now that we have the Street, we can move onto finding the City. Similar to what we did before, we need to use a combination of the substring and string position functions. 

In plain English:

Return a substring starting one character after the first comma all the way to the next comma. 

In our tag, we would put:


Breaking down where we see the colons:

  • substr = return a substring

  • ($projectteam_762|strpos:","+1) = starting position

  • (($projectteam_762|substr:($projectteam_762|strpos:","+1))|strpos:",") = end position

Output the "State / Zip"

Finally, all we have left is the state and zip code. This time, we will use a variation of our string position function because we want to start reading the right right side. We will use:


That will start from right to left to find the first comma. Then since we do not care to stop the text, we don't need to add a second parameter to the "substr" function. Our final tag will be:


That says, using field related to $projectteam_762, return a substring starting from the right side of the string until the first comma until the string is done. 


Full Address: {$projectteam_762}

Street Only: {$projectteam_762|substr:0:($projectteam_762|strpos:",")}

City Only: {$projectteam_762|substr:($projectteam_762|strpos:","+1):(($projectteam_762|substr:($projectteam_762|strpos:","+1))|strpos:",")}

State Zip Only: {$projectteam_762|substr:($projectteam_762|strrpos:","+1)}