【发布时间】: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>
【问题讨论】:
-
看看in_array()和similar_text()
-
或者,如果您想亲自动手,请查看Damerau-Levenshtein distance 或simlar algorithms...