user3522738 user3522738 - 4 months ago 17
PHP Question

php explode to split and combine reverse string

text left - text right


How to swap right to left using php? str pos is not so good because the pattern is not always fix. It could be
somethingleft-somethingright
or
more left text here - right


The result that I want should look like
text right - text left


tried explode() but it isn't always single character at the left or right side.

Answer

Simply can use explode(). Example here

$str = "text left - text right";
$exp = explode('-', $str);
$newStr = trim($exp[1]) . ' - ' . trim($exp[0]);
echo $newStr;