刽子手“词”在 PHP 中

发布于 2024-08-17 21:07:47 字数 598 浏览 4 评论 0原文

我正在尝试做一些类似于刽子手的事情,当你猜测一个字母时,它会用该字母替换下划线。我已经想出了一个方法,但似乎效率很低,我想知道是否有更好的方法。这是我所拥有的 -

<?
$word = 'ball';
$lettersGuessed = array('b','a');

echo str_replace( $lettersGuessed , '_' , $word ); // __ll

echo '<br>';

$wordArray = str_split ( $word );

foreach ( $wordArray as $letterCheck )
{

    if ( in_array( $letterCheck, $lettersGuessed ) )
    {
        $finalWord .= $letterCheck;
    } else {
        $finalWord .= '_';
    }

}

echo $finalWord; // ba__
?>

str_replace 的做法与我想要的相反。我想要 $finalWord 的值,而不必通过循环来获得我想要的结果。

I am trying to do something similar to hangman where when you guess a letter, it replaces an underscore with what the letter is. I have come up with a way, but it seems very inefficient and I am wondering if there is a better way. Here is what I have -

<?
$word = 'ball';
$lettersGuessed = array('b','a');

echo str_replace( $lettersGuessed , '_' , $word ); // __ll

echo '<br>';

$wordArray = str_split ( $word );

foreach ( $wordArray as $letterCheck )
{

    if ( in_array( $letterCheck, $lettersGuessed ) )
    {
        $finalWord .= $letterCheck;
    } else {
        $finalWord .= '_';
    }

}

echo $finalWord; // ba__
?>

str_replace does the opposite of what I want. I want what the value of $finalWord is without having to go through a loop to get the result I desire.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心是晴朗的。 2024-08-24 21:07:47

如果我按照你的说法正确,你想要做与第一行相反的事情:

echo str_replace( $lettersGuessed , '_' , $word ); // __ll

为什么不创建一个 $opposite = range('a', 'z'); 数组,然后使用 array_diff () 与 $lettersGuessed 相对应,这将为您提供一组未猜出的字母。它肯定会节省几行代码。例如:

$all_letters = range('a', 'z');
$unguessed = array_diff ($all_letters, $lettersGuessed);
echo str_replace( $unguessed , '_' , $word ); // ba__

If I am following you right you want to do the opposite of the first line:

echo str_replace( $lettersGuessed , '_' , $word ); // __ll

Why not create an array of $opposite = range('a', 'z'); and then use array_diff () against $lettersGuessed, which will give you an array of unguessed letters. It would certainly save a few lines of code. Such as:

$all_letters = range('a', 'z');
$unguessed = array_diff ($all_letters, $lettersGuessed);
echo str_replace( $unguessed , '_' , $word ); // ba__
意中人 2024-08-24 21:07:47

它是一个数组,foreach 是你应该做的事情,无论如何它都快如闪电,我认为你正在沉迷于一些甚至不是问题的东西。

您想要使用数组,因为您可以轻松判断数组中的哪些索引包含该字母,这直接与字符串中 _ 应该成为字母的位置相关。

It's an array, foreach is what you're suppose to be doing, it's lightning fast anyways, I think you are obsessing over something that's not even a problem.

You want to use an array becuase you can easily tell which indexes in the array are the ones that contain the letter, which directly correlates to which place in the string the _ should become a letter.

吻安 2024-08-24 21:07:47

你的 foreach 循环是一个很好的方法。它不会很慢,因为你的话永远不会很大。

您还可以使用猜测的字母创建正则表达式模式来替换除这些字母之外的所有内容。像这样:

$word = 'ball';
$lettersGuessed = array('b','a');
$pattern = '/[^' . implode('', $lettersGuessed) . ']/';   // results in '/[^ba]/
$maskedWord = preg_replace($pattern, '_', $word);
echo $maskedWord;

Your foreach loop is a fine way to do it. It won't be slow because your words will never be huge.

You can also create a regex pattern with the guessed letters to replace everything except those letters. Like this:

$word = 'ball';
$lettersGuessed = array('b','a');
$pattern = '/[^' . implode('', $lettersGuessed) . ']/';   // results in '/[^ba]/
$maskedWord = preg_replace($pattern, '_', $word);
echo $maskedWord;
辞慾 2024-08-24 21:07:47

另一种方法是将字符串作为数组访问,例如

$word = 'ball';
$length = strlen($word);
$mask = str_pad('', $length, '_');
$guessed = 'l';

for($i = 0; $i < $length; $i++) {
    if($word[$i] === $guessed) {
        $mask[$i] = $guessed;
    }
}
echo $mask; // __ll

Another way would be to access the string as an array, e.g.

$word = 'ball';
$length = strlen($word);
$mask = str_pad('', $length, '_');
$guessed = 'l';

for($i = 0; $i < $length; $i++) {
    if($word[$i] === $guessed) {
        $mask[$i] = $guessed;
    }
}
echo $mask; // __ll
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文