|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Aurelou Smarty n00b
Joined: 23 Mar 2021 Posts: 2
|
Posted: Tue Mar 23, 2021 10:18 am Post subject: Add a line break after 3 words |
|
|
Hello,
Here is my actual code with truncate:
Code: | <a href="{$product.url}">{$product.name|truncate:50:'...'}</a>
|
I want to change it, I don't want to use truncate, I need to add a line break in $product.name after 3 words.
I found this php code which allows to do it :
Code: | <?php
$test = 'one two three four five six seven eight nine ten eleven tweleve thirteen fourteen';
echo preg_replace('~((\w+\s){3})~', '$1' . "\n", $test);
?> |
Can you please help me translate it into smarty in order to make it work with my code? I think in smarty it's not preg_replace but regex_replace. Or if you have another solution to make a line break after 3 words...
Thank you very much for your help, I am a novice. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Mar 24, 2021 4:28 am Post subject: |
|
|
Try:
Code: | {$name_array = " "|explode:$product.name}{foreach $name_array as $name}{if $name@index eq 3}<br>{/if}{$name} {/foreach} |
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Mar 24, 2021 10:21 pm Post subject: |
|
|
So, this is MOD(3)?
This will break after every third word. |
|
Back to top |
|
Aurelou Smarty n00b
Joined: 23 Mar 2021 Posts: 2
|
Posted: Thu Mar 25, 2021 1:53 pm Post subject: |
|
|
A BIG thank you, it works perfectly! |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Mar 25, 2021 2:15 pm Post subject: |
|
|
bsmither wrote: | So, this is MOD(3)?
This will break after every third word. |
Which was exactly the intent. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|