在php上用星号计算单词

在php上用星号计算单词,php,Php,我有一个用星号字符计算单词的代码,结果如下: Sentence "Today is Monday" have : Count word = 3 word Word-1 :Today Word-2 :is Word-3 :Monday Today is Monday count word = 3 word * * * * * = Today * * = is * * * * * * = Monday 和源代码: <?php $sentence = "Today is Monday";

我有一个用星号字符计算单词的代码,结果如下:

Sentence "Today is Monday" have :
Count word = 3 word
Word-1 :Today
Word-2 :is
Word-3 :Monday
Today is Monday
count word = 3 word

* * * * * = Today
* * = is
* * * * * * = 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>";
?>
和功能应该可以帮助您做到这一点:

foreach ($word as $w) {
    echo sprintf('%s = %s<br>', str_repeat('* ', strlen($w)), $w);
}
foreach($word作为$w){
回显sprintf(“%s=%s
”,str_repeat(“*”,strlen($w)),$w); }
尝试下面的代码

$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>";
你也可以试试这个

 <?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++;
 }
 ?>

不是最好的,但工作正常

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>";
}
函数星($word){

对于($x=1;$x另一个建议是

<?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 = "";
    }
?>
试试这个:

<?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]);
    }

echo str_repeat('*',strlen($word[0])。“=”$word[0]。“
”;
为什么要将$句子分解两次?
$word=explode(“,$句子);$count=count($word);
请使用上述代码,它将提供与您所需相同的输出。@Dini Dwi Rahayupase给出您的反馈。@Dini Dwi Rahayu
<?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]);
    }