Ilia Ross
Ilia Ross

Reputation: 13404

Take away the first word from the string

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?

Upvotes: 0

Views: 4128

Answers (2)

chris
chris

Reputation: 36937

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;
?>

Upvotes: 3

nickb
nickb

Reputation: 59699

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);

Upvotes: 8

Related Questions