Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Created April 7, 2013 20:11
Show Gist options
  • Save swvitaliy/5332279 to your computer and use it in GitHub Desktop.
Save swvitaliy/5332279 to your computer and use it in GitHub Desktop.
Функция. Возвращает значение гет параметра из урла или указанное в 3м параметре дефолтное значение.
<?php
/**
* Возвращает значение гет параметра из урла или указанное в 3м параметре дефолтное значение.
*
* Example: url_get_arg('http://test.ru/test/?a=6&b=533&c=100', 'c', NULL);
*/
function url_get_arg($url, $name, $default = NULL) {
$q_pos = strpos($url, '?');
if ($q_pos === FALSE) {
return $default;
}
$get_args = substr($url, $q_pos + 1);
$get_args = explode('&', $get_args);
$kw_args = array();
foreach($get_args as $v) {
$eq_pos = strpos($v, '=');
if ($eq_pos === FALSE) {
$kw_args[$v] = '';
} else {
$kw_args[substr($v, 0, $eq_pos)] = substr($v, $eq_pos + 1);
}
}
return isset($kw_args[$name]) ? $kw_args[$name] : $default;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment