Tag Archive for parameter

URI Segment

<?php

function segment($index) {
	$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

	$path = (strpos($path, '/') == 0) ? ltrim($path, '/') : $path ;

	$path_chunked = explode('/', $path);

	return ($index > count($path_chunked) ) ? FALSE : $path_chunked[$index-1];
}

?>

source

Pass a stream to a function #2

void myfunction(ifstream &fp, ...)  // use this
void myfunction(ifstream fp, ...)   // not this

source

URL Parameter to Page Include

<?php
header("Content-type: text/html;charset=utf-8");
if(!isset($description))
{
$description = 'Default description for your site';
}

if(!isset($title))
{
$title = 'Default page title for your site';
}

?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php
if (isset($_GET['page']) && $_GET['page'] != "") {
$page = $_GET['page'];
if (file_exists('sections/'.$page.'.php')) {
@include ('sections/'.$page.'.php');
} elseif (!file_exists('sections/'.$page.'.php')) {
echo '404 error.'; //A quick and simple means to produce a 404 page through template.
}
} else {
@include ('sections/landing.php');
}
?>
</body>
</html>

source

Array Splitter (random => odd & even)

//get an array and by using it, create two different array which are odd and even
void arraySplitter(int *&array, int size, int &sizeEven, int &sizeOdd, int *&array2)
{
int even = 0;
int odd = 0;

//determine how many member of given array is even and how many of them is odd
for (int i = 0; i < size; i++)
{
if(array[i]%2==0)
{
even++;
}
else
{
odd++;
}
}

//by using even and odd parameter, creates two different array
//which will be used soon
int *arrayOdd= new int[even];
int *arrayEven = new int[odd];

int m = 0;
int n = 0;

//creates arrays which has only even or odd members
for (int i = 0; i < size; i++)
{
if(array[i]%2==0)
{
arrayEven[m] = array[i];
m++;
}
else
{
arrayOdd[n] = array[i];
n++;
}
}

//returns arrays with pass by referance
array = arrayEven;
array2 = arrayOdd;
}

source

like URL parser and manipulator

/**
* @projectDescription 	Poly9's polyvalent URLParser class
*
* Modified to URL reconstruction by Mikko Ohtamaa.
*
* @authorDenis Laprise <denis@poly9.com>, Mikko Ohtamaa <mikko.ohtamaa@twinapex.com>
*
* @version	0.2
* @namespace	Poly9
*
* How to query URL:
*
* <pre>
* var p = new Poly9.URLParser('http://user:password@poly9.com/pathname?arguments=1#fragment');
* p.getHost() == 'poly9.com';
* p.getProtocol() == 'http';
* p.getPathname() == '/pathname';
* p.getQuerystring() == 'arguments=1';
* p.getFragment() == 'fragment';
* p.getUsername() == 'user';
* p.getPassword() == 'password';
* </pre>
*
* How to manipulate URL:
*
* <pre>
* var p = new Poly9.URLParser("http://localhost:8080/path);
*
* p.setQuerystring("foo=bar");
* p.setFragment("anchor");
* p.setPort(7070);
*
* var url = p.getURL() // <a href="http://localhost:7070/path?foo=bar#anchor" >http://localhost:7070/path?foo=bar#anchor</a>
* </pre>
*
* See the unit test file for more examples.
* URLParser is freely distributable under the terms of an MIT-style license.
*
* The orignal code: <a href="https://code.poly9.com/trac/browser/urlparser/urlparser.js" >https://code.poly9.com/trac/browser/urlparser/urlparser.js</a>
*
* <a href="http://poly9.com" >http://poly9.com</a>
*
* <a href="http://www.twinapex.com" >http://www.twinapex.com</a>
*/

if (typeof Poly9 == 'undefined')
var Poly9 = {};

/**
* Creates an URLParser instance
*
* @classDescription	Creates an URLParser instance
* @return {Object}	return an URLParser object
* @param {String} url	The url to parse
* @constructor
* @exception {String}  Throws an exception if the specified url is invalid
*/
Poly9.URLParser = function(url) {
this._fields = {'Username' : 4, 'Password' : 5, 'Port' : 7, 'Protocol' : 2, 'Host' : 6, 'Pathname' : 8, 'URL' : 0, 'Querystring' : 9, 'Fragment' : 10};
this._values = {};
this._regex = null;
this.version = 0.2;

// MO: Fixed fragments are allowed to contain dash
this._regex = /^((w+)://)?((w+):?(w+)?@)?([^/?:]+):?(d+)?(/?[^?#]+)???([^#]+)?#?([^#]*)/;
for (var f in this._fields) {
this['get' + f] = this._makeGetter(f);
this['set' + f] = this._makeSetter(f);
}

// Patch in getURL, since otherwise field generators override this
this.getURL = this._getURL;

if (typeof url != 'undefined')
this._parse(url);
}

/**
* @method
* @param {String} url	The url to parse
* @exception {String} 	Throws an exception if the specified url is invalid
*/
Poly9.URLParser.prototype.setURL = function(url) {
this._parse(url);
}

/**
* Rebuild the URL to string form
*
* @method
* @return {String} Reconstructed full URL
*/
Poly9.URLParser.prototype._getURL = function() {
var s;

s = this.getProtocol();

s += "://"

if(this.getUsername() != '') {
s += this.getUsername();
}

if(this.getPassword() != '') {
s += ":";
s += this.getPassword();
}

if(this.getUsername() != '') {
s += "@";
}

s += this.getHost();

if(this.getPort() != '') {
s += ":" + this.getPort();
}

s += this.getPathname();

if(this.getQuerystring() != '') {
s += "?" + this.getQuerystring();
}

if(this.getFragment() != '') {
s += "#" + this.getFragment();
}

return s;
}

Poly9.URLParser.prototype._initValues = function() {
for(var f in this._fields)
this._values[f] = '';
}

Poly9.URLParser.prototype._parse = function(url) {
this._initValues();
var r = this._regex.exec(url);
if (!r) throw "DPURLParser::_parse -> Invalid URL"
for(var f in this._fields) if (typeof r[this._fields[f]] != 'undefined')
this._values[f] = r[this._fields[f]];
}

Poly9.URLParser.prototype._makeGetter = function(field) {
return function() {
return this._values[field];
}
}

Poly9.URLParser.prototype._makeSetter = function(field) {
return function(value) {
this._values[field] = value;
}
}

source

displaying parameter passed from url

<?php
$something = $_GET['something'];
echo "hello, $something";
?>

source

order/format of params in method definition

def name(params, hash, *array, &proc );
puts params.inspect, hash.inspect, array.inspect, proc.inspect
end

name(1, 2=>3, 4=>5, *[6,7,8]) {9} #disambiguate array
puts
name(1, {2=>3, 4=>5}, 6,7,8) {9} #disambiguate hash
puts
name(1, {2=>3, 4=>5}, 6,7,8, &lambda {9}) #called with lambda instead of block
puts
name(1, 2=>3, 4=>5, *[6,7,8], &Proc.new {9}) #or proc instead of block

# 1
# {2=>3, 4=>5}
# [6, 7, 8]
# #<Proc:...>

source

Devuelve el Querystring sin un parametro indicado

function querystringWithoutParameter($parametro){
return preg_replace('/[&?]+'.$parametro.'=[^&]*/','',$_SERVER['QUERY_STRING']);
}

source