Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,772 answers

573 users

How to check if second string is subsequence of first string in PHP

1 Answer

0 votes
function isSecondStringSubsequenceOfFirstString($first, $second) {
    $i = 0;
    $j = 0;
    while ($i < strlen($first)) {
        if ($j >= strlen($second)) {
            return false;
        }
        if ($first[$i] == $second[$j]) {
            echo "first[$i] = " . $first[$i] . " second[$j] = " . $second[$j];
            echo " i = " . $i . " j = " . $j . "\n";
            $i++;
        }
        $j++;
    }
    return true;
}

$first = "php programming";
$second = "php pro programming";

echo isSecondStringSubsequenceOfFirstString($first, $second) ? "yes" : "no";



 
/*
run:
 
first[0] = p second[0] = p i = 0 j = 0
first[1] = h second[1] = h i = 1 j = 1
first[2] = p second[2] = p i = 2 j = 2
first[3] =   second[3] =   i = 3 j = 3
first[4] = p second[4] = p i = 4 j = 4
first[5] = r second[5] = r i = 5 j = 5
first[6] = o second[6] = o i = 6 j = 6
first[7] = g second[11] = g i = 7 j = 11
first[8] = r second[12] = r i = 8 j = 12
first[9] = a second[13] = a i = 9 j = 13
first[10] = m second[14] = m i = 10 j = 14
first[11] = m second[15] = m i = 11 j = 15
first[12] = i second[16] = i i = 12 j = 16
first[13] = n second[17] = n i = 13 j = 17
first[14] = g second[18] = g i = 14 j = 18
yes
  
*/

 





answered Mar 24 by avibootz
...