note 92396 deleted from function.array-search by levim
| From: | levim@php.net | Date: | Mon, 24 Feb 2014 22:43:46 +0000 |
| Subject: | note 92396 deleted from function.array-search by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198695@lists.php.net to get a copy of this message | ||
Note Submitter: mikeytown2
----
Made a function that I used to search the $_SERVER variable ( http://php.net/reserved.variables.server ) for
matches to getcwd ( http://php.net/getcwd ) excluding some
key's that I don't want.
<?php
/**
* Returns all key/values in array that match.
*
* @param $needle
* What your searching for
* @param $haystack
* Array of values
* @param $a_not
* Optional array of key names to exclude
*/
function boost_array_find($needle, $haystack, $a_not = array()) {
$out = array();
foreach($haystack as $key=>$value) {
if (strpos($value, $needle) !== FALSE) {
$good = TRUE;
foreach($a_not as $not) {
if (strpos($key, $not) !== FALSE) {
$good = FALSE;
}
}
if ($good) {
$out[$key] = $value;
}
}
}
return $out;
}
// Example Usage
$document_root = str_replace("\\", '/', getcwd());
$rejects = array('SCRIPT_FILENAME');
$out = boost_array_find($document_root, $_SERVER, $rejects);
print_r($out);
?>
Which should output this on most servers.
Array
(
[DOCUMENT_ROOT] => path/to/webroot
)
On Mosso's servers it will output this
Array
(
[PHP_DOCUMENT_ROOT] => path/to/webroot
)