0

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?

4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by asif49
2

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

Edited by TySkby: n/a

Votes + Comments
Offered great help!
1

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';
}

Edited by vimalnath53: n/a

Votes + Comments
Offered good help!
0

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

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.