这是我给出的起点:
<?php
function capitaliseLastLetterOfEachWord( $string ){
return $string;
}
echo capitaliseLastLetterOfEachWord("The quick brown fox jumps over the lazy dog");
?>
这是我得到的,它给了我大写的每个单词的最后一个字母:
<?php
function capitaliseLastLetterOfEachWord( $string ){
$separate = explode(" ", $string);
foreach($separate as $word) {
$lastword = $word[strlen($word) - 1 ];
echo strtoupper($lastword) . "<br>";
}
}
echo capitaliseLastLetterOfEachWord("The quick brown fox jumps over the lazy dog");
?>
答案 0 :(得分:2)
其他人已经给出了简短的方法,只是为了解释你现在正在做什么,你只是改变了最后一个字符串而没有返回它。您需要定位最后一个字符,然后进行更改,只需&
添加对foreach的引用,然后不要忘记返回值:
$lastword = $word[strlen($word) - 1 ];
// you're not returning and appending your changes
<?php
function capitaliseLastLetterOfEachWord( $string ){
$separate = explode(" ", strtotlower($string));
foreach($separate as &$word) { // add reference
$word[strlen($word) - 1] = strtoupper($word[strlen($word) - 1]);
// point to it ^ , the change ^
}
return implode(' ', $separate); // don't forget to return
}
echo capitaliseLastLetterOfEachWord("The quick brown fox jumps over the lazy dog");
答案 1 :(得分:1)
function capitaliseLastLetterOfEachWord( $string ){
return strrev(ucwords(strrev($string)));
}
步骤:
如果您想首先删除任何现有资本,请将其更改为此
return strrev(ucwords(strrev(strtolower($string))));