0

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.

  • You can perform a logical OR match on multiple possibilities by separating them with a `|`. For example, the regular expression `/^(hello|hi)$/` will perform an exact match on the strings `hello` and `hi`, but fail for any other string like `hel`, `helloo`, and `his`. – B. Fleming Dec 18 '18 at 19:34
  • I'd say the first step you need to take is: you need to be able to clearly say _either_ the full set of exact phrases to test against _or_ a clear method by which to tell what you consider a match. In it's current form the question leaves that open which is why it cannot really be answered. – arkascha Dec 18 '18 at 19:38
  • If you want fuzzy matching, regex isn't going to do it for you. Checkout `soundex()`, `metaphone()`, `similar_text()`, or `levenshtein()`. – Alex Howansky Dec 18 '18 at 19:38

4 Answers4

1

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\n" : "false\n");
}

Output:

Hello: true 
Helloooo: true 
How r youuuu: true 
hell: false 
w are y: false 
heey hello: true 
heeeey hello bro: true

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
0

Here's a way to do it. As far as I can see you want it to be case-insensitive that's what the strtolowers 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;
}
Enthys
  • 301
  • 3
  • 11
0

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.

nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

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]}'."
       ) . "\n"
  ;
}

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.
*/
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42