我有一个代码来计算带有星号字符的单词,结果我喜欢这样:
Sentence "Today is Monday" have :
Count word = 3 word
Word-1 :Today
Word-2 :is
Word-3 :Monday
和源代码:
<?php
$sentence = "Today is Monday";
$word=explode(" ", $sentence);
$count = count(explode(" ", $sentence));
//$count2 = count(explode(" ", $word));
echo "Sentence <b>\"".$sentence."\"</b> have : <br>";
echo "Count word = $count<br>\n";
echo "Word-1 :".$word[0]."<br>";
echo "Word-2 :".$word[1]."<br>";
echo "Word-3 :".$word[2]."<br>";
?>
结果,我想这样:
Today is Monday
count word = 3 word
* * * * * = Today
* * = is
* * * * * * = Monday
答案 0 :(得分:0)
str_repeat和strlen函数可以帮助您实现这一目标:
foreach ($word as $w) {
echo sprintf('%s = %s<br>', str_repeat('* ', strlen($w)), $w);
}
答案 1 :(得分:0)
尝试以下代码。
$sentence = "Today is Monday";
$word=explode(" ", $sentence);
$count = count(explode(" ", $sentence));
//$count2 = count(explode(" ", $word));
echo "Sentence <b>\"".$sentence."\"</b> have : <br>";
echo "Count word = $count<br>\n";
echo str_repeat('*', strlen($word[0])) . " = " . $word[0] . "<br>";
echo str_repeat('*', strlen($word[1])) . " = " . $word[1] . "<br>";
echo str_repeat('*', strlen($word[2])) . " = " . $word[2] . "<br>";
<强>输出:强>
Sentence "Today is Monday" have :
Count word = 3
***** = Today
** = is
****** = Monday
答案 2 :(得分:0)
你也可以试试这个
<?php $sentence = "Today is Monday";
$word=explode(" ", $sentence);
$count = sizeof($word);
echo "Sentence <b>\"".$sentence."\"</b> have : <br>";
echo "Count word = $count<br>\n";
$i=0;
while($i<$count){
echo str_repeat('*', strlen($word[$i])) . " = " . $word[$i] . "<br>";
$i++;
}
?>
答案 3 :(得分:0)
不是最好的,但按预期工作
function star($word){
for ($x = 1; $x <= strlen($word); $x++) {
$star[] = "*";
}
return implode("",$star)." = ".$word;
}
$sentence = "Today is Monday";
$word=explode(" ", $sentence);
$count = count(explode(" ", $sentence));
//$count2 = count(explode(" ", $word));
echo "Sentence <b>\"".$sentence."\"</b> have : <br>";
echo "Count word = $count<br>\n";
foreach($word as $words){
echo star($words)."<br>";
}
答案 4 :(得分:0)
另一个建议可能是
<?php
$sentence = "Today is Monday";
$arrayWords = explode(" ", $sentence);
$cptWords = count($arrayWords);
$showAnswer = "Count word = $cptWords word";
if($cptWords>1) $showAnswer.= "s"; //add "s to "word" if more than 1 word in $sentence
$showAnswer .= "<br />";
$currentStarWord = "";
//embolding one word in two
for($i=0; $i<$cptWords; $i++){
if($i%2==0) echo "<strong>$arrayWords[$i]</strong> ";
else echo $arrayWords[$i]." ";
}
echo "<br />";
echo "$showAnswer<br />";
for($i=0; $i<$cptWords; $i++){
for($j=0; $j<strlen($arrayWords[$i]); $j++){
$currentStarWord .= "*";
}
echo "$currentStarWord = <strong>$arrayWords[$i]</strong><br />";
$currentStarWord = "";
}
?>
答案 5 :(得分:0)
试试这个:
<?php
function replaceWithCharacter($s, $character='* ')
{
return str_repeat($character, strlen($s));
}
$sentence = 'Today is monday';
$words = str_word_count($sentence, 1);
$x = array_map('replaceWithCharacter', $words);
$len = count($words);
echo $sentence."\n";
echo sprintf("count word = %d word%s\n\n", $len, (1 < $len ? 's' : ''));
for ($i=0; $i<$len; $i++) {
echo sprintf("%s = %s\n", $x[$i], $words[$i]);
}