I have a string group like ("hello", "hi", "how r u", "how are you", "how r you", "how are u", etc.) and i want to create a function that can compare a string varible (like $varible = "Helloooo") with string group. Regex or any method is useful to me.
String can be more but can not be missing for example:
Hello = should be true
Helloooo = should be true
How r youuu !!!! = should be true
hell = should be false
w are y = should be false
heey hello = should be true
heeeey hello bro = should be true
I'm talking about this string group ("hello", "hi", "how r u", "how are you", "how r you", "how are u", etc.)
I say "string group" because type doesn't have to be an array but it also may be an array. As i said any method is useful to me.
Thanks for you support.
Here's a way to do it. As far as I can see you want it to be case-insensitive that's what the strtolower
s are for
$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;
}
In your case you only have to check if the supplied word is a dictionary word or not. More technically, it is whether the supplied word has any of the dictionary words as it's sub sequence. If there exists a word in dictionary as a sub sequence of the supplied word, we return true, else we return 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;
}
Output:
Hello => bool(true)
Helloooo => bool(true)
How r youuu !!!! => bool(true)
hell => bool(false)
w are y => bool(false)
See Demo.
This function returns the array index of the first match or -1
if not found.
$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
$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]}'."
) . "
"
;
}
result:
/*
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.
*/
You can create a regexp to match your strings by imploding the array with |
(regexp OR operator) and testing that against each value with preg_match
. Note that we add the i
modifier to the regexp to make it case-insensitive:
$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
" : "false
");
}
Output:
Hello: true
Helloooo: true
How r youuuu: true
hell: false
w are y: false
heey hello: true
heeeey hello bro: true