regex - similar substring in other string PHP -
how check substrings in php prefix or postfix. example, have search string named $to_search
follows:
$to_search = "abcdef"
and 3 cases check if substring in $to_search follows:
$cases = ["abc def", "def", "deff", ... other values ...];
now have detect first 3 cases using substr()
function. how can detect "abc def", "def", "deff"
substring of "abcdef"
in php.
to find of cases either begin or end either beginning or ending of search string, don't know of way step through of possible beginning , ending combinations , check them. there's better way this, should it.
$to_search = "abcdef"; $cases = ["abc def", "def", "deff", "otherabc", "noabcmatch", "nodefmatch"]; $matches = array(); $len = strlen($to_search); ($i=1; $i <= $len; $i++) { // beginning , end of search string of length $i $pre_post = array(); $pre_post[] = substr($to_search, 0, $i); $pre_post[] = substr($to_search, -$i); foreach ($cases $case) { // beginning , end of each case of length $i $pre = substr($case, 0, $i); $post = substr($case, -$i); // check if of them match if (in_array($pre, $pre_post) || in_array($post, $pre_post)) { // using case array key $matches keep distinct $matches[$case] = true; } } } // use array_keys() keys values var_dump(array_keys($matches));
Comments
Post a Comment