Jump to content

I can not find the match; please advice me.


Go to solution Solved by Muddy_Funster,

Recommended Posts

<?php

	$fruitvegi= array(
	   'A' => 'Achoccha|Amaranth|Angelica|Anise|Apple|Arrowroot|Arrugula',
	   'B' => 'Balsam Apple|Balsam Pear -- see under Momordica|Bambara groundnut|Bamboo|Banana and Plantains|Barbados Cherry|Beans|Beet|Blackberry|Blueberry|Bok Choy|Boniato|Broccoli|Broccoli|Broccoli|Brussels|Bunch|Burdock',
       'E' => 'Eggplant|Endive|Eugenia',
       'W' => 'Water celery|Waterchestnut|Watercress|Watermelon' );

	$sentence = "There are watermelon in the garden";

	$pieces1 = explode(' ', $sentence);
	
	foreach($pieces1 as $element1) {
		echo "<h2>". $element1."</h2>";
		
		
		while(list($key, $val) = each($fruitvegi)) { 

			$pieces = explode('|', $val);
			
			foreach($pieces as $element) {
			echo $element.'<br/>';
			
			if( $element==$element1 ) {
				
				echo "<H1> $key  Match </h1>";
				exit();
				
				
			}
			
	
		}
	
		} 

		
	}

?>

I am trying to match the word( eg: watermelon ) from the sentence variable ($sentence) to words in the $fruitvegi array. And get the array key value.

But I am not getting the result.

Can you please advice what  I am doing wrong.

Edited by Jayram121

1) you know, using the first letter of the word, which main element in $fruitvegi you need to look in. there's no need to scan through the entire array each time.

 

2) see Psycho's reply above about case-insensitive

 

3) what exact output are you trying to get (an example would be nice) and are you trying to get it for all words that are found or only the first one that is found?

Edited by mac_gyver

I was also going to add that if you normalized the $fruitvegi array to be lowercase and forced the input to be lowercase you could just use in_array() to check for a match in the sub arrays rather than doing a foreach loop over all the elements.

<code>
<?php

	$fruitvegi= array(
	   'a' => 'achoccha|amaranth|angelica|anise|apple|arrowroot|arrugula',
	   'b' => 'balsam apple|balsam pear -- see under momordica|bambara groundnut|bamboo|banana and plantains|barbados cherry|beans|beet|blackberry|blueberry|bok choy|boniato|broccoli|broccoli|broccoli|brussels|bunch|burdock',
       'e' => 'eggplant|endive|eugenia',
       'w' => 'water celery|waterchestnut|watercress|watermelon' );

	$sentence = "there are watermelon in the garden";

	$pieces1 = explode(' ', $sentence);
	
	foreach($pieces1 as $element1) {
		
		if (in_array($element1, $fruitvegi)) {
			echo "Match";
			exit();
		}
	}

?>
</code>

I cannot able to get it work

I am only after the array key

So answer should be => Array Key - W

given that your $fruitvegi array contains entries that have multiple words in them, your assignment isn't as simple as exploding the $sentence on the spaces and finding if any single word is in the $fruitvegi array.

 

what is the actual statement of your assignment?

Assignment - Get the mobile devices manufacturers name.

 

In $phoneDevices array;  named key - manufacturers names

<?php 

	$phoneDevices = array(
			'iPhone'        => '\biPhone.*Mobile|\biPod', // |\biTunes
			'BlackBerry'    => 'BlackBerry|\bBB10\b|rim[0-9]+',
			'HTC'           => 'HTC|HTC.*(Sensation|Evo|Vision|Explorer|6800|8100|8900|A7272|S510e|C110e|Legend|Desire|T8282)|APX515CKT|Qtek9090|APA9292KT|HD_mini|Sensation.*Z710e|PG86100|Z715e|Desire.*(A8181|HD)|ADR6200|ADR6400L|ADR6425|001HT|Inspire 4G|Android.*\bEVO\b',
			'Nexus'         => 'Nexus One|Nexus S|Galaxy.*Nexus|Android.*Nexus.*Mobile',
			// @todo: Is 'Dell Streak' a tablet or a phone? 
			'Dell'          => 'Dell.*Streak|Dell.*Aero|Dell.*Venue|DELL.*Venue Pro|Dell Flash|Dell Smoke|Dell Mini 3iX|XCD28|XCD35|\b001DL\b|\b101DL\b|\bGS01\b',
			'Samsung'       => 'Samsung|SGH-I337|BGT-S5230|GT-B2100|GT-B2700|GT-B2710|GT-B3210|GT-B3310|GT-B3410|GT-B3730|GT-B3740|GT-B5510|GT-B5512|GT-B5722|GT-B6520|GT-B7300|GT-B7320|GT-B7330|GT-B7350|GT-B7510|GT-S5350|GT-S5360|GT-S5369|GT-S5380|GT-S5380D|GT-S5560|GT-S5570|GT-S5600|GT-S5603|GT-S5610|GT-S5620|GT-S5660|GT-S5670|GT-S5690|GT-S5750|GT-S5780|GT-S5830|GT-S5839|GT-S6102|GT-S6500|GT-S7070|GT-S7200|GT-S7220|GT-S7230|GT-S7233|GT-S7250|GT-S7500|GT-S7530|GT-S7550|GT-S7562|GT-S7710|SPH-M920|SPH-M930|SPH-N100|SPH-N200|SPH-N240|SPH-N300|SPH-N400|SPH-Z400|SWC-E100|SCH-i909|GT-N7100|GT-N7105|SCH-I535',
			'Sony'          => 'SonyST|SonyLT|SonyEricsson|SonyEricssonLT15iv|LT18i|E10i|LT28h|SonyEricssonMT27i',
			'Asus'          => 'Asus.*Galaxy|PadFone.*Mobile',
		);
?>

Get the Mobile devices Contents using User-Agent

 

<?php

$userAgent = $_SERVER['HTTP_USER_AGENT'];

?>

If it Samsung  S4 mobile devise I will the user_agent result.

 

Mozilla/5.0 (Linux; U; Android 2.3.6; en-gb; GT-S5360 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 Irix

 

If you look at the result ‘GT-S5360’ is the one;  I want to match it in the  $phoneDevices array.

Get the array key value.

The answer is Samsung.

  • Solution

You will need to explode each array value on the pipe delimiter and run through that for a match, something like the following (untested code):

$currentUserAgent = explode(" ", $_SERVER['HTTP_USER_AGENT']);
foreach($phoneDevices as $manufact=>$agentList){
  $agantArray = explode('|', $agentList);
  $arrayComp = array_intersect($agentArray, $currentUserAgent);
  if(count($arrayComp >= 1)){
    $yourMan = $manufact;
    break 1;
  }
}
if(!isset($yourMan)){
  echo "No match found";
}
else{
  echo "Match found, you are using a $yourMan device";}
Edited by Muddy_Funster
  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.