【问题标题】:How to Compare related strings?如何比较相关字符串?
【发布时间】:2014-07-11 20:11:32
【问题描述】:

所以我的程序有问题,我在一个名为 ArrName 的数组中搜索一个名字,然后如果他/她存在于数组中则显示英俊或漂亮,但如果该名称不在数组中,则程序将显示相关如果我搜索的字符串在数组中不存在,则为字符串。我很难解决这个问题,请帮忙。这是我的例子。

示例输出 #1:

姓名: Bob、John、Charlie、Bravo、Alpha、Raymond、Kenneth、Rose、Rosel、James

正在搜索:鲍勃。

结果:鲍勃存在,鲍勃很帅

样本输出#2:

姓名: Bob、John、Charlie、Bravo、Alpha、Raymond、Kenneth、Rose、Rosel、James

正在搜索:罗杰。

结果:未找到 Roger。你的意思是:

玫瑰?

罗塞尔?

到目前为止,这是我的代码:

    <html>
<body>
    <?php
        $arrNumbers = array('jonel','jon','john','rosel','rosil','rose','ramon','ramin','ramoon','kenneth','keneth','kennet','joy','juy');
        $arrboys = array('jonel','jon','john','ramon','ramin','ramoon','kenneth','keneth','kennet');
        $arrgirls = array('rosel','rosil','rose','joy','juy');
        $strsearch = 'rosel';
        $count = 0;
        $badd = 0;
        $gadd = 0;

        foreach($arrNumbers as $value)// checks if name exists
        {
            if($value == $strsearch)
            {
                $count = $count + 1;

            }
            else
            {
                $count = $count + 0;

            }
        }

        if($count > 0)// if name exists, checks if the name is from a boy or a girl
        {
            //boys
            foreach($arrboys as $value)// checks if the name is from the boys
            {
                if($value == $strsearch)
                {
                    $badd = $badd + 1;
                }
                else
                {
                    $badd = $badd + 0;
                }
            }
            if($badd > 0)
            {
                echo $strsearch.' is handsome';
            }

            // girls
            foreach($arrgirls as $value)// checks if the name is from the girls
            {
                if($value == $strsearch)
                {
                    $gadd = $gadd + 1;
                }
                else
                {
                    $gadd = $gadd + 0;
                }
            }
            if($gadd > 0)
            {
                echo $strsearch.' is beautiful';
            }
        }
        else// if the name does not exists
        {
          // this is the part where i dont know what to do.
          // this part suppose to display all the names related to the name that is 
          // being searched.
            echo 'did you mean';
        }
    ?>
</body>

【问题讨论】:

标签: php arrays


【解决方案1】:

为什么要检查 $arrNumbers 数组中的出现,因为 $arrboys 和 $arrgirls 是 $arrNumbers 的子字符串。

使用下面的代码

<?php
        $arrNumbers = array('jonel','jon','john','rosel','rosil','rose','ramon','ramin','ramoon','kenneth','keneth','kennet','joy','juy');
        $arrboys = array('jonel','jon','john','ramon','ramin','ramoon','kenneth','keneth','kennet');
        $arrgirls = array('rosel','rosil','rose','joy','juy');
        $strsearch = 'rosel';
        $flag = '';
        for($i=0;$i<sizeof($arrboys);$i++)
        {
            //echo $arrboys[$i].' ';
            if($strsearch == $arrboys[$i]);
            $flag = 'boy';
        }

        for($i=0;$i<sizeof($arrgirls);$i++)
        {
            //echo $arrboys[$i].' ';
            if($strsearch == $arrgirls[$i]);
            $flag = 'girl';
        }
        if($flag == 'boy')
        {
            echo $strsearch.' exist, '. $strsearch.' is handsome';
        }
        if($flag == 'girl')
        {
            echo $strsearch.' exist, '. $strsearch.' is beautiful';
        }
        else
        {
            echo  $strsearch.' not found. did you mean:';
        }
        ?>

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 2013-01-06
    • 2023-03-15
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2011-08-17
    • 2010-10-19
    • 2011-10-20
    相关资源
    最近更新 更多