arrays - Exchanging the first word & the last word in PHP -


how can exchange first letter , last letter in php ?

for e.g, if string "hello", result must "oellh". used str_replace , can replace 1 word. here code:

$string="hello"; $first=$string[0]; $last=substr($string, -1); $result=str_replace($first, $last, $string); echo $result; 

try this:

$string="hello"; $first=$string[0]; //get first character $last=$string[strlen($string)-1]; //get last character $string[0] =  $last; // set last character first $string[strlen($string)-1] =$first;  //  set first character last echo $string; 

output:

oellh 

live test


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -