XF 2.2 Convert string to array in template

Azzl

New member
How would I convert this string to an array within a template?

{{ $records.connectionString }} which equals "0:0:991:1000 0:1:235:1"

I'd like to convert the string above to an array (which is broken up by " " and ":", then loop through it and only output the third value 991 and 235 on their own lines.

EX:
991
235

Also note: Apologies if this is in the wrong section.
 
Haven't tested it but perhaps you can use the split filter on the variable?

 
If you're needing to do this inside the template, you could use something like this...

Code:
<xf:set var="$connectionArray" value="{{ $records.connectionString|split(' ') }}" />

<xf:foreach loop="$connectionArray" value="$connection">
    <xf:set var="$connectionParts" value="{{ $connection|split(':') }}" />
    <span class="connectionThird">{$connectionParts.2}</span>
</xf:foreach>

That said, I'd recommend setting all of this up in PHP and storing it as an array in a template var.
 
If you're needing to do this inside the template, you could use something like this...

Code:
<xf:set var="$connectionArray" value="{{ $records.connectionString|split(' ') }}" />

<xf:foreach loop="$connectionArray" value="$connection">
    <xf:set var="$connectionParts" value="{{ $connection|split(':') }}" />
    <span class="connectionThird">{$connectionParts.2}</span>
</xf:foreach>

That said, I'd recommend setting all of this up in PHP and storing it as an array in a template var.
That's what I ended up doing, thank you very much for your feedback!
 
Back
Top Bottom