PDA

View Full Version : PHP Help



Coda
08-03-2009, 07:58 PM
Hey i am trying to compare 2 strings and check what word/words are common for e.g.:



$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 :)

Agnostic Bear
08-03-2009, 11:01 PM
Hey i am trying to compare 2 strings and check what word/words are common for e.g.:



$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:



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 > 1 ) {
echo $key . ' with ' . $value . ' occurences.<br />';
}
}
?>


now in function form!



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, 1 );

foreach( $compare as $key => $value ) {
echo $key . ' with ' . $value . ' occurences.<br />';
}
?>

Coda
09-03-2009, 03:40 PM
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.:



$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.

Fehm
09-03-2009, 04:45 PM
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.:



$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.



$compare = compareForCommonWords( $string1, $string2, 1 );

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

Coda
09-03-2009, 05:18 PM
Nope, i have tried it and it still displays if "bob" appears in string1 twice, and none in string2

Dentafrice
10-03-2009, 10:37 PM
Try this. This gets all the words, and checks to see if they are in both of the strings.



<?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 />";
}
?>

Protege
11-03-2009, 10:35 AM
Id of done this:



<?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

Protege
11-03-2009, 11:01 AM
Improved code, I believe:


<?php
function StrCommon( $Str, $Str2 )
{
$Array[] = explode( ' ', $Str );
$Array[] = explode( ' ', $Str2 );

array_multisort( $Array, SORT_DESC );

$Count = array();

for( $i = 0; $i < sizeof( $Array[ 0 ] ); $i++ )
{
foreach( $Array[ 0 ] as $Word )
{
if( $Word === $Array[ 1 ][ $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 />';
}
}

?>

Want to hide these adverts? Register an account for free!