[ create a new paste ] login | about

Link: http://codepad.org/nTE8QWkf    [ raw code | output | fork ]

PHP, pasted on Dec 6:
<?php
$str = '
what
are
you
doing';

$dict_arr = explode("\n", $str);
$rack_arr = str_split('what')


?>
<pre>
<?php print_r(findwordswithany($dict_arr, $rack_arr)); ?>
</pre>
<pre>
<?php print_r(findwordswithall($dict_arr, $rack_arr));?>
</pre>
<?php


function findwordswithany($dict_arr, $rack_arr){
	$found_arr = array();
	foreach($dict_arr as $word){	
		foreach($rack_arr as $letter){
			if(strpos($word, $letter) !== false){				
				$found_arr[] = $word;
				break 1;
			}
		}	
	}
	if(count($found_arr) > 0)
		return $found_arr;
	else
		return false;
}

function findwordswithall($dict_arr, $rack_arr){
	$found_arr = array();
	foreach($dict_arr as $word){	
		$foundall = true;
		foreach($rack_arr as $letter){
			if(strpos($word, $letter) === false){				
				$foundall = false;
				break 1;
			}
		}
		if($foundall === true){
		$found_arr[] = $word;
		}
	}
	if(count($found_arr) > 0)
		return $found_arr;
	else
		return false;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
<pre>
Array
(
    [0] => what
    [1] => are
)
</pre>
<pre>
Array
(
    [0] => what
)
</pre>


Create a new paste based on this one


Comments: