I'm not really good with arrays and have had a go at this, but to no avail.

I have a string called $str and another called $area.

I need to split the string $str into an array of words (delimiter being spaces). This is the part I was able to do...

The next bit is to get the longest word from $str which is also in $area and store it within any new variable.

Can anyone do this?

Recommended Answers

All 5 Replies

If I'm reading this correctly, you have been able to split the string variable $str into an array. Assuming you have named the new array $str_array, you could do something like this:

-Iterate through the array and for each value in $str_array, use strlen() to count the number of characters in the current value
-Compare it to the existing longest value (we'll call it $longest). If it is larger, check if it's in the $area array.
-If it is, replace the value of $longest to be the value of the current iteration.

With this method, you'd come out with something similar to the following code:

//Set the $longest variable to be an empty string for now
$longest = '';

//Iterate through $str_array with the current iteration set to $value
foreach($str_array as $value)
{
	//Get the length of $value
	$length_value = strlen($value);
	//Get the length of $longest
	$length_longest = strlen($longest);
	
	//Compare the lengths
	if ($length_value > $length_longest)
	{
		//$value has more characters than $longest, so check if it is in $area
		if (in_array($value, $area))
		{
			//$value is a value of $area array, so make it the new value for $longest
			$longest = $value;
		}
	}
}

At the end of this loop, $longest will be equal to the longest word that exists in both $str_array and $area arrays.

Obviously this could be shortened down to be more efficient and such (and I would encourage you to do that), but I figured it would help more people reading this thread if it was a bit more spelled-out.

Hope it helps!

-Ty

commented: Offered great help! +3

Hi,
Just Try this:

<?php 
$words = array();
$wordCnt = array();
$str = "this is my damn world";
$area = "That part of the world is safe";
//spilting area to an array 
echo 'given text 1 =>'.$str;
echo '<br>';
echo 'given text 2 =>'.$area;
$new_str = explode(" ",$str);
foreach($new_str as $key => $value) {
	$words[] =  $value;
	$wordCnt[] = strlen($value);
	}
$result = array_combine($words,$wordCnt);
//finding the longest word
foreach($result as $key => $value){
	if($value == max($wordCnt)) {
		$wordSearch = $key;
	}	
}
$new_area = explode(" ",$area);
//check if the longest word exists in the another string
$res = in_array($wordSearch,$new_area);
if($res){
	echo '<br>'.'the longest word is :'.$wordSearch;
} else{
	echo '<br>'.'No matching records found';
}
commented: Offered good help! +3

Thanks all for the replies, I have done it already but hopefully they'll aid other people who are in the same position.

+1 to both

Member Avatar for diafol

I was going to stick my oar in as it's not marked as solved. So is it?

Looks like I forgot to do that, cheers for reminding me ardav. Marked as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.