0

How to remove the first word away in Smarty from a string, that looks like:

$variable = "Word Quick brown fox";

And make it look like this afterwards on the output:

$variable = "Quick brown fox";

Is there a way to do this without modifiers? I know you can make custom modifier and then use $variable|customModifier and get what you want but I thought maybe there is already inbuilt solution for that?

j08691
  • 204,283
  • 31
  • 260
  • 272
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
  • 3
    There s not a builtin modifier for this. One reason for this is that this type of stuff should be done in the business logic (via PHP, of course). Smarty's purpose to provide a mechanism for creating templates and laying out pages, not doing string manipulation like this. – Tyler Crompton Aug 29 '12 at 17:19
  • Thank you, Tyler, I realize that. I will have to make Modifier then. – Ilia Ross Aug 29 '12 at 17:22

2 Answers2

8

As Tyler points out, this should be done in your business logic, like so:

  1. explode() on a space to create an array of words
  2. Remove the first entry with array_shift() (corresponding to the first word in the sentence)
  3. Join it back together with implode()

Here is an example:

$words = explode( ' ', $variable);
array_shift( $words);
$variable = implode( ' ', $words);
nickb
  • 59,313
  • 13
  • 108
  • 143
  • 1
    It looks like OP wants the first letter to be capitalized too. Also, you should put a limit of 2 in the `explode` function as `explode` will count consecutive delimiters as one delimiter. So if the string contains multiple consecutive spaces after the first word, all but one space in each group will be lost. – Tyler Crompton Aug 29 '12 at 17:24
  • I will do this. If I use Modifier it means I do it, like Tyler says `in the business logic`? – Ilia Ross Aug 29 '12 at 17:25
  • @Tyler I thought that the first word should be caps at first, but if you look in the original string, the first two words are capitalized, so I left that alone. Also, for consecutive spaces, a better solution would be to use `preg_split()`, however since that requirement wasn't in the OP, I omitted it for clarity. – nickb Aug 29 '12 at 17:25
  • Ah, looked over that. What I am saying, though, is that simply, putting `, 2` just before the closing parenthesis in the `explode` function, it should fix the problem I described. – Tyler Crompton Aug 29 '12 at 17:27
  • Thank you nickb, I used `array_shift` eventually. It works like I expected it! Greetings! – Ilia Ross Aug 29 '12 at 17:40
2

Explode the original string into an array via the space between the words, then slice out the chosen index key. Then implode the array back to a string.

<?php
$variable = "Word Quick brown fox";
$str = explode(' ', $variable);
$str = array_slice($str, 1);
$str = implode(' ', $str);
echo $str;
?>
chris
  • 36,115
  • 52
  • 143
  • 252
  • 1
    Thanks Chris, like I said above I would write modifier for this but I solved it in PHP by `array_shift`. – Ilia Ross Aug 29 '12 at 17:39