note 92396 added to function.array-search
| From: | mikeytown2 at osu1 dot php dot net | Date: | Wed, 22 Jul 2009 23:14:20 +0000 |
| Subject: | note 92396 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158599@lists.php.net to get a copy of this message | ||
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. Made this function in response to this http://drupal.org/node/482000 & Mosso's server
configuration. Will allow for a more generalized solution.
<?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
)
----
Server IP: 64.71.164.2
Probable Submitter: 24.16.70.195
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/92396
Del: integrated -- https://master.php.net/note/delete/92396/integrated
Del: useless -- https://master.php.net/note/delete/92396/useless
Del: bad code -- https://master.php.net/note/delete/92396/bad+code
Del: spam -- https://master.php.net/note/delete/92396/spam
Del: non-english -- https://master.php.net/note/delete/92396/non-english
Del: in docs -- https://master.php.net/note/delete/92396/in+docs
Del: other reasons-- https://master.php.net/note/delete/92396
Reject -- https://master.php.net/note/reject/92396
Search -- https://master.php.net/manage/user-notes.php