Php 在数组中查找匹配字符串或在字符串中查找单词
我很难创建一个函数,在添加之前检查数据库中是否存在“字符串”项,如果不存在,它将分解字符串并找到匹配的字符串。这就是我所做的,但是错过了 更新信息:我在比较输入值和数据库值时遇到了这个问题 $database=“i'm;”$input=“im”当我使用下面的函数比较这两个值时,结果是空的,因为在item not found中。但是当我尝试重置$input=“i'm”的值时,当然可以找到item。即使使用string\u替换,也可以基本上删除两个值上都不是字母或数字的任何内容。我需要帮助Php 在数组中查找匹配字符串或在字符串中查找单词,php,Php,我很难创建一个函数,在添加之前检查数据库中是否存在“字符串”项,如果不存在,它将分解字符串并找到匹配的字符串。这就是我所做的,但是错过了 更新信息:我在比较输入值和数据库值时遇到了这个问题 $database=“i'm;”$input=“im”当我使用下面的函数比较这两个值时,结果是空的,因为在item not found中。但是当我尝试重置$input=“i'm”的值时,当然可以找到item。即使使用string\u替换,也可以基本上删除两个值上都不是字母或数字的任何内容。我需要帮助 //st
//string_replace is to replace all spaces and special chars with ""
public function match_string(){
$name = $this->string_replace($this->name);
$all_item = $this->find_all(); // find_all is to fetch all data from mysql table
foreach($all_item as $item){
$lowercase = $this->string_replace($item->name);
if($lowercase == $name){
// match found
}elseif($item_explode=explode(" ",$item->name)){
$name_explode = explode(" ",$this->name);
foreach($name_explode as $key){
if(in_array($key,$item_explode)){
//match key found
}
}
}
}
}
string\u replace
不是php中的函数。您应该使用str\u replace
方法,可以在这里找到:要检查字符串中是否有单词,请使用
if (strpos($word, $my_string) !== false)
{
echo 'Word found';
}
string\u replace
是OP创建的一个函数。我猜string\u replace
是OP类的一个方法。函数string\u replace是自己创建的函数,用于删除所有空格和特殊字符。我尝试使用strpos,但不断出现空分隔符错误