Can I split $site->title()?

I am wondering if I can split something like $site->title()

Basically I want to be able to wrap the last word in a span so I can color it.

Something like (I know this is completely wrong, it’s an example of what I want to achieve).

<?=$site->title()[0] ?> <span class="red"><?=$site->title()[1] ?></span>

Sure:

$titleParts = $page->title()->split(' ');
dump($titleParts);
$lastElement = $titleParts[count($titleParts) - 1];

See I tried that but all I get is

Error: Array to string conversion

Did you try to echo an array, maybe?

You can do like that with supports multiple spaces:

if (strpos($site->title()->value(), ' ') !== false && $titleArray = $site->title()->split(' ')) {
    echo array_shift($titleArray) . ' <span class="red">' . implode(' ', $titleArray) . '</span>';
} else {
    echo $site->title();
}

Cool thanks - I’ll work with this for now (it works)

<?= e(strpos($site->title()->value(), ' ') !== false && $titleArray = $site->title()->split(' '), array_shift($titleArray) . ' <span class="red">' . implode(' ', $titleArray) . '</span>') ?>

@ahmetbora Hm, but that wraps everything but the first element in the span…

I’m not suggest using the e() method. Use direct if() because if title contains a space you’ll get error.

Yes, just to supports multiple words. Can use array_pop() may be.

@texnixe My site title is only two words, so it’s fine for now. It’s just a development site after all.

@ahmetbora The site title already has a space in it? It’s two words ‘Site Title’ for example, so why would it error?

You can test with Title as single word and e() method.

Ah yes, ok. I’ll fix, thanks.