我有一个字符串组(“hello”,“hi”,“how ru”,“你好吗”,“你怎么样”,“你好吗”等),我想创建一个可以创建的函数将字符串变量(如$ varible =“Helloooo”)与字符串组进行比较。正则表达式或任何方法对我有用。
字符串可以更多但不能缺少例如:
你好=应该是真的
Helloooo =应该是真的
你怎么样!!!! =应该是真的
地狱=应该是假的
w是y =应该是假的
heey hello =应该是真的
heeeey hello bro =应该是真的
我在谈论这个字符串组(“你好”,“你好”,“你好吗”,“你好吗”,“你怎么样”,“你好吗”等等)
我说“字符串组”因为类型不必是一个数组,但它也可能是一个数组。正如我所说,任何方法对我都有用。
感谢你的支持。
这是一种方法。据我所知,你希望它不区分大小写,这就是strtolower
s的用途
$parts = ['hello', 'hi', 'how r u', 'how are you', 'how r you', 'how are u'];
function wordContainsPart($parts, $word) {
$word = strtolower($word);
foreach($parts as $part){
$part = strtolower($part);
if(strpos($word, $part) !== false) {
return true;
}
}
return false;
}
在您的情况下,您只需要检查提供的单词是否是字典单词。更具技术性的是,提供的单词是否具有任何字典单词,因为它是子序列。如果在字典中存在一个单词作为所提供单词的子序列,则返回true,否则返回false。
<?php
$dictionary = array("hello", "hi", "how r u", "how are you", "how r you", "how are u");
function isDictionaryWord($str,$dictionary){
foreach($dictionary as $each_word){
if(isSubsequence(strtolower($each_word),strtolower($str))){
return true;
}
}
return false;
}
function isSubsequence($needle,$haystack){
$len1 = strlen($needle);
$len2 = strlen($haystack);
if($len1 > $len2) return false;
$ptr = 0;
for($i=0;$i<$len2 && $ptr < $len1;$i++){
if($haystack[$i] === $needle[$ptr]) $ptr++;
}
return $ptr == $len1;
}
$tests = array(
'Hello',
'Helloooo',
'How r youuu !!!!',
'hell',
'w are y'
);
foreach($tests as $each_test){
echo $each_test," => ",var_dump(isDictionaryWord($each_test,$dictionary)),PHP_EOL;
}
输出:
Hello => bool(true)
Helloooo => bool(true)
How r youuu !!!! => bool(true)
hell => bool(false)
w are y => bool(false)
见Demo。
此函数返回第一个匹配的数组索引,如果未找到则返回-1
。
$array = ['hello', 'hi', 'how r u', 'how are you', 'how r you', 'how are u'];
function findStartString(array $startStrHaystack, string $needle)
{
foreach ($startStrHaystack as $idx => $startString)
if(0 === substr_compare($startString, $needle, 0, strlen($startString), true))
return $idx;
return -1;
}
测试用例
$testCase = ['Hello', 'Helloooo', 'How r youuuu', 'hell', 'w are y'];
foreach ($testCase as $testString)
{
$idx = findStartString($array, $testString);
echo "The start of '$testString' "
. ( $idx < 0
? 'does not match any string in given array.'
: "matches '{$array[$idx]}'."
) . "\n"
;
}
结果:
/*
The start of 'Hello' matches 'hello'.
The start of 'Helloooo' matches 'hello'.
The start of 'How r youuuu' matches 'how r you'.
The start of 'hell' does not match any string in given array.
The start of 'w are y' does not match any string in given array.
*/
您可以通过使用|
(regexp OR运算符)对数组进行内爆,并使用preg_match
对每个值进行测试来创建一个匹配字符串的正则表达式。请注意,我们将i
修饰符添加到regexp以使其不区分大小写:
$parts = ['hello', 'hi', 'how r u', 'how are you', 'how r you', 'how are u'];
$strings = ['Hello', 'Helloooo', 'How r youuuu', 'hell', 'w are y', 'heey hello', 'heeeey hello bro'];
$regexp = implode('|', $parts);
foreach ($strings as $string) {
echo "$string: " . (preg_match("/($regexp)/i", $string) ? "true\n" : "false\n");
}
输出:
Hello: true
Helloooo: true
How r youuuu: true
hell: false
w are y: false
heey hello: true
heeeey hello bro: true