[SOLVED] string manipulation A-B…-X-Y to Y-A-B-…-X

Issue

I have a string in this format:

Each substring is seperated by ‘-‘

A-B-C…-X-Y

My question is how to move the last substring to the first as

Y-A-B-C…-X

in php

Thanks a lot.

Solution

Here’s some code that’ll do it:

// Split the string into an array
$letters = explode('-', 'A-B-C-X-Y');

// Pop off the last letter
$last_letter = array_pop($letters);

// Concatenate and rejoin the letters
$result = $last_letter . '-' . implode('-', $letters);

Answered By – Michael Louis Thaler

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *