Results 1 to 8 of 8

Thread: PHP Help

  1. #1
    Join Date
    Aug 2008
    Posts
    36
    Tokens
    0

    Default PHP Help

    Hey i am trying to compare 2 strings and check what word/words are common for e.g.:

    PHP Code:
    $string1 "hello my name is bob";
    $string2 "today bob went to the park"
    now i want it to find that "bob" is most common in the 2 strings.
    I have tried researching up on strcmp but i don't think that is what i need for this.

    Any help is appreciated thanks

  2. #2
    Join Date
    Oct 2006
    Location
    Peterborough, UK
    Posts
    3,855
    Tokens
    216

    Latest Awards:

    Default

    Quote Originally Posted by Coda View Post
    Hey i am trying to compare 2 strings and check what word/words are common for e.g.:

    PHP Code:
    $string1 "hello my name is bob";
    $string2 "today bob went to the park"
    now i want it to find that "bob" is most common in the 2 strings.
    I have tried researching up on strcmp but i don't think that is what i need for this.

    Any help is appreciated thanks
    Something like this should do what you want:

    PHP Code:
    Common words:<br />
    <?php
    $string1 
    "hello my name is bob";
    $string2 "today bob went to the park";

    $arr[] = explode' '$string1 );
    $arr[] = explode' '$string2 );

    $hold = array();

    $count count$arr );

    for( 
    $i 0$i $count$i++ ) {
        foreach( 
    $arr$i ] as $key ) {
            if( 
    array_key_exists$key$hold ) ) {
                
    $hold$key ]++;
            } else {
                
    $hold$key ] = 1;
            }
        }
    }

    arsort$hold );

    foreach( 
    $hold as $key => $value ) {
        if( 
    $value ) {
            echo 
    $key ' with ' $value ' occurences.<br />';
        }
    }
    ?>
    now in function form!

    PHP Code:
    Common words:<br />
    <?php
    function compareForCommonWords$string1$string2$minAmount ) {
        
        
    $arr[] = explode' '$string1 );
        
    $arr[] = explode' '$string2 );
        
        
    $hold = array();
        
        
    $count count$arr );
        
        for( 
    $i 0$i $count$i++ ) {
            foreach( 
    $arr$i ] as $key ) {
                if( 
    array_key_exists$key$hold ) ) {
                    
    $hold$key ]++;
                } else {
                    
    $hold$key ] = 1;
                }
            }
        }
        
        
    arsort$hold );
        
        foreach( 
    $hold as $key => $value ) {
            if( 
    $value $minAmount ) {
                
    $compare$key ] = $value;
            }
        }
        
        
    arsort$compare );
        
        return 
    $compare;
    }

    $string1 "hello my name is bob";
    $string2 "today bob went to the park";

    $compare compareForCommonWords$string1$string2);

    foreach( 
    $compare as $key => $value ) {
        echo 
    $key ' with ' $value ' occurences.<br />';
    }
    ?>
    Last edited by Jewish Bear; 08-03-2009 at 11:05 PM.


    visit my internet web site on the internet
    http://dong.engineer/
    it is just videos by bill wurtz videos you have been warned

  3. #3
    Join Date
    Aug 2008
    Posts
    36
    Tokens
    0

    Default

    Thanks that worked perfectly, only problem now is that i want it to only show if the words are in both of the strings for e.g.:

    PHP Code:
    $string1 "bob is cool bob";
    $string2 "i am ok"
    That would still say there is 2 occurances however they are not in both of the strings so display nothing. I want it to only display if they occur in both the strings.

    If that makes any sense? i can't really explain it very well lol.

  4. #4
    Join Date
    Apr 2008
    Location
    Derby
    Posts
    4,668
    Tokens
    262

    Latest Awards:

    Default

    Quote Originally Posted by Coda View Post
    Thanks that worked perfectly, only problem now is that i want it to only show if the words are in both of the strings for e.g.:

    PHP Code:
    $string1 "bob is cool bob";
    $string2 "i am ok"
    That would still say there is 2 occurances however they are not in both of the strings so display nothing. I want it to only display if they occur in both the strings.

    If that makes any sense? i can't really explain it very well lol.

    PHP Code:
    $compare compareForCommonWords$string1$string2); 
    That line would stop it from showing the 2 because it compares, so it should work But then again, I dont know lol im crap at PHP, just my idea?
    lol
    Back for a while

  5. #5
    Join Date
    Aug 2008
    Posts
    36
    Tokens
    0

    Default

    Nope, i have tried it and it still displays if "bob" appears in string1 twice, and none in string2

  6. #6
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    Try this. This gets all the words, and checks to see if they are in both of the strings.

    PHP Code:
    <?php
    function common_words$string1$string2 ) {

        
    $words["string1"] = explode(' '$string1); // gets the words for the first string.
        
    $words["string2"] = explode(' '$string2); // gets the words for the second string.

        /* Goes through each of the words and checks to see if they are in the other string. */
        /* If they are, we update the counter. */
        
    foreach($words["string1"] as $word) {
            if(
    in_array($word$words["string2"])) {
                
    $words_data[$word] += 1// updates the counter for the word.
            
    }
        }
        
        return 
    $words_data// returns the data.

    }

    $string1 "hello my name is bob";
    $string2 "hello today bob went to the park";

    $compare common_words($string1$string2);

    foreach(
    $compare as $key => $value) {
        echo 
    "{$key} appeared {$value} times in both strings. <br />";
    }
    ?>

  7. #7
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Id of done this:

    PHP Code:
    <?php
    function StrCommon$Str$Str2 )
    {
        
    $ExplodeStr explode' '$Str );
        
    $ExplodeStr2 explode' '$Str2 );

        
    // Checks which is longer and puts it first in the check. (Probably not needed)

        
    if( sizeof$ExplodeStr ) < sizeof$ExplodeStr2 ) )
        {
            
    $x $ExplodeStr2;
            
    $y $ExplodeStr;
        }
        else
        {
            
    $x $ExplodeStr;
            
    $y $ExplodeStr2;
        }
        
        
    $Count = array();
        
        for( 
    $i 0$i sizeof$x ); $i++ )
        {
            
    // $Count[ $x[ $i ] ]++;
            
    foreach( $y as $Word )
            {
                if( 
    $Word === $x$i ] )
                {
                    
    $Count$x$i ] ]++;
                }
            }
        }
        
        return 
    $Count;
    }

    $string1 "hello my name is bob";
    $string2 "hello today bob went to the park";

    $Common StrCommon$string1$string2 );

    foreach( 
    $Common as $Word => $Count )
    {
        if( 
    max$Common ) === $Count )
        {
            echo 
    'The most common word is <b>' $Word '</b> with a count of ' $Count '<br />';
        }
    }

    ?>
    $string1 = "hello my name is bob";
    $string2 = "hello today bob went to the park";

    The most common word is hello with a count of 1
    The most common word is bob with a count of 1
    $string1 = "bob is cool bob";
    $string2 = "i am ok";

    Nothing
    $string1 = "James Rozee is testing something called James";
    $string2 = "Something is being tested by James Rozee";

    The most common word is James with a count of 2
    $string1 = "Something is being tested by James Rozee";
    $string2 = "James Rozee is testing something called James";

    The most common word is James with a count of 2
    Hi, names James. I am a web developer.

  8. #8
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Improved code, I believe:
    PHP Code:
    <?php
    function StrCommon$Str$Str2 )
    {
        
    $Array[] = explode' '$Str );
        
    $Array[] = explode' '$Str2 );
        
        
    array_multisort$ArraySORT_DESC );
        
        
    $Count = array();
        
        for( 
    $i 0$i sizeof$Array] ); $i++ )
        {
            foreach( 
    $Array] as $Word )
            {
                if( 
    $Word === $Array][ $i ] )
                {
                    
    $Count$Word ]++;
                }
            }
        }
        
        return 
    $Count;
    }

    $string1 "hello my name is bob";
    $string2 "hello today bob went to the park";

    $Common StrCommon$string1$string2 );

    foreach( 
    $Common as $Word => $Count )
    {
        if( 
    max$Common ) === $Count )
        {
            echo 
    'The most common word is <b>' $Word '</b> with a count of ' $Count '<br />';
        }
    }

    ?>
    Last edited by Protege; 11-03-2009 at 11:08 AM.
    Hi, names James. I am a web developer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •