如何使用单个字符搜索字符串?

时间:2017-03-13 14:58:17

标签: php arrays string

我正在根据用户在开头输入的名称创建猜测同义词游戏。现在我对值$word, $str, $clue

进行了硬编码

我在表格中显示单词,每行有两个字符串。一个是提示同义词是什么的线索,然后是它自己的同义词。

所以,我的生活不能/不知道如何将单个字符与一串字符进行比较。我试过strpos(), strstr()但比较字母的字母:'Con'rad - 'Con'cept。

我正在寻找的是下面的内容:

  • Conrad将字母C与单词Concept
  • 中的所有字符进行比较
  • 康拉德将字母O与支持
  • 中的所有字符进行比较
  • 康拉德将字母N与设计
  • 中的所有字符进行比较
  • 康拉德将字母R与单词Fresh
  • 中的所有字符进行比较
  • Conrad将字母A与单词Aged
  • 中的所有字符进行比较
  • 康拉德将字母D与单词Glad
  • 中的所有字符进行比较

这是代码:

<!DOCTYPE html>
<html>
<head>
<title>Show Me</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">

<style>
    table{
        margin: 0 auto;
    }
    th{
        background-color:#CAD704;
        padding: 10px;
        text-align: left;}
    td{
        vertical-align: middle;
        padding-left: 5px;
        }

    tr:nth-child(even) {background: #ECF0CB}
    tr:nth-child(odd) {background: #F6F8E7}

    .floating-box {
        float: left;
        width: 30px;
        height: 30px;
        margin: 5px;

    }

    input[type="text"] {
        width: 25px;
        height: 25px;
    }
    input.char[type="text"] {
        width: 25px;
        height: 25px;
        background-color : #d1d1d1;
    } 
    .button {
        background-color: #76AEEB; /* Green */
        border: 1px solid black;
        color: white;
        padding: 15px 32px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        margin: 15px 2px;
        cursor: pointer;
        width: 150px;
    }       

    .sub {border-radius: 12px;}     
</style>
<?php 

$word ="Conrad";

$str = array("Concept", "Support", "Design", "Fresh", "Aged","Glad");
$clue = array("Idea", "Keep", "Make","New", "Old", "Happy");

echo $test = implode(" ", $str) , " <b>Implode</b> <br />";
echo $cluestr = implode(" ", $clue) , " <b>Implode</b> <br />";

$strlen = strlen($test);
$strlen2 = strlen($cluestr);

echo $count = count($str). " elements in array <br />";
echo $count2 = count($clue). " elements in array <br />";

echo $strlen , " string length<br />";
echo $strlen2 , " string length<br />";

echo $test , " String <br />";
echo $cluestr , " String <br /><br />";


?>  


 <div id="content">
 <table>
 <tr>
     <th>Clue</th><th colspan="100">Synonym</th>
    </tr> 
     <tr>
         <!-- This is the start -->
 <?php  
  $p = 0;       
  $h = 0;
    for( $i = 0; $i <= $strlen; $i++ ) {

            if ($p == 0){
                echo "<td> $clue[$h]</td>";
                $h++;
            }
        $charW = substr( $word, $i, 1 );    
        $char = substr( $test, $i, 1 );

        if($char == " " || $char ==""){ 
        echo"
            <td style='visibility: hidden;'>
                <div class='floating-box'>
                    <input type='text' maxlength='1'>
                </div>
            </td>
            <td style='visibility: hidden;'>
                <div class='floating-box'>
                    <input type='text' maxlength='1'>
                </div>
            </td>
        </tr>
        <tr>";
          $p=0;
         continue; 
        }else {
            echo "<td>
                    <div class='floating-box'>
                        <input type='text' value='$char' maxlength='1' class='char'>
                    </div>
                </td>";
        }
        $p++;
    }


       ?>            
         <!-- The end -->

     </tr>
    </table>
    </div>
    </body>

如果某些事情没有意义,请告诉我,我会尽力澄清一下。

2 个答案:

答案 0 :(得分:2)

这里有一个简单的实现:

$string = "conrad";
$array = ['Concept', 'Support', 'Design', 'Fresh', 'Aged', 'Glad'];

for ($i = 0, $n = strlen($string); $i < $n; $i++) {
    if (strstr(strtolower($array[$i]), strtolower($string[$i]))) {
        echo $string[$i] . " belongs to " . $array[$i] . "\n";
    } else {
        echo "fail\n";
    }
}

输出:

c belongs to Concept
o belongs to Support
n belongs to Design
r belongs to Fresh
a belongs to Aged
d belongs to Glad

实时样本:https://3v4l.org/k9A5K

答案 1 :(得分:2)

这是另一种选择:

$word  = 'conrad';
$array = ['Concept', 'Support', 'Design', 'Fresh', 'Aged', 'Glad'];

foreach(str_split($word) as $position => $letter) {
    if(stripos($array[$position], $letter) !== false) {
        echo "$letter found in $array[$position]<br>\n";
    }
}
  • 将单词拆分为字母并将其循环,露出位置
  • 检查同一位置的单词中的字母是否不区分大小写