Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Count the number of matching characters in two strings

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 555
    Comment on it

    There are different ways to compare two strings in PHP whether by using built-in functions like strcmp or strcasecmp of PHP or using equality operator(==). None of these methods return the number of matching characters in both the strings.

    This article demonstrate a PHP function similar_text() which returns the number of matching characters of both strings and can also return percentage of similarity between two strings by passing a optional parameter. This is case-sensitive function.

     

    Syntax : similar_text(string1, string2, additional_variable)

    Description :

    string1 : Specify first string for comparison (Required).
    string2 : Specify second string for comparison (Required).
    additional_variable : Specify the percent variable used to store percentage of similarity (Required).

    Example1 :

    <?php
       $str1 = "Hello User";
       $str2 = "Hello Guest";
       echo similar_text($str1, $str2);
    ?>
    Output : 7

     

    Example 2 :   

    <?php
       $str1 = "Hello User";
       $str2 = "Hello Guest";
       similar_text($str1, $str2, $percent);
       echo "Percentage of similarity : ".round($percent)."%";
    ?>

     

    Output : Percentage of similarity : 67%

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: