UrlParser full code stable

From: Date: Sat, 04 Jan 2003 17:12:15 +0000
Subject: UrlParser full code stable
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-12097@lists.php.net to get a copy of this message
It appear be stable. Please, take a look and vote on it. Also, I am sending a test script. <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Roberto Berto <darkelder.php.net> | // +----------------------------------------------------------------------+ // // $Id$ /** * UrlParser fake URLs turned into data * * * ------------------------------------------------------------------------- * LICENSE (LGPL) * Copyright (C) 2002-2003 Roberto Berto * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Or at http://www.gnu.org/licenses/lgpl.txt Get a fake URL like * http://www.example.com/index.php/section/fruits/subsection/orange * and turn into: array("section" => "fruits", "subsection" => "orange") * ------------------------------------------------------------------------- * * * Also, UrlParser can can add the array to $_REQUEST. and/or to * $_GET and/or $_POST. * * From a given URL (http://www.example.com/path/to/sub/path) or Path * (/path/to/sub/path) it turn path=to and sub=path, from the right * to left. Even numbers is the key and odd numbers is the values. * * If the number of subpaths isn't a even, it will forget the * last one. Eg: /iAmAlone/fruit/orange/color/red will be turned * into fruit=orange and color=red and iAmAlone will not be * used. * * If no URL or Path was set, it will try to guess the current * URL from PHP ENV. * * Note that it works with full URL and with PATHs too. * * * METHODS: * array parse([string URL]) * return an key=>val array with the URL or from PHP ENV * * bool putGet(array) * will merge the given UrlParser array with the $_GET var * * bool putRequest(array) * will merge the given UrlParser array with $_REQUEST var * * bool putPost(array) * merge the given UrlParser array with $_POST * * bool putAll(array) * merge given UrlParser array with $_GET, $_REQUEST and $_POST * but will not put on Global - see putGlobal() * * bool putGlobal(array) * will put each key to global scope (not recommended) * * USAGE / Example: * $url = "http://www.example.to/me/Name/she/HerName/Section/Hat"; * require_once "UrlParser/UrlParser.php"; * * $urlParsed = UrlParser::parse($url); * print_r($urlParsed); * * UrlParser::putGet($urlParsed); * print $_GET["me"]; // will print "Name" */ class UrlParser { /** * Parse the URL * * Parse and return an associative array. It will parse * * @param string $url URL or Path to turn into an associative array * @return array a key=>value array of the URL function * parse($url = null) */ function parse($url = null) { // guessing current URL if (empty($url)) { $url = $_SERVER["PATH_INFO"]; } // cant have a / at the end if (substr($url,-1,1) == "/") { $url = substr($url,0,strlen($url) - 1); } // remove http://domain part of the URL and use only the part $urlparsed = parse_url($url); // spliting Url by / $elements = explode("/",$urlparsed["path"]); // getting the size $elementsSize = sizeof($elements); // going two per two for ($i = 0; $i < floor($elementsSize / 2); $i++) { // key is the last element less actual element less two $key = $elements[$elementsSize - ($i) * 2 - 2]; // val is the last element less actual element less one // using urldecode to remove encoded url things $val = urldecode($elements[$elementsSize - ($i) * 2 - 1]); // only set both is set if ($val && $key) { // setting to be returned $parsed[$key] = $val; } } return $parsed; } /** * Put UrlParsed to global scope * * Will put each key to global scope. * * Note that due to security problems its not recommended to put * user defined things to global scope. * * @param array $parsed parsed() array * @return bool true on success else false */ function putGlobal($parsed = array()) { if (sizeof($parsed) == 0) { return false; } foreach ($parsed as $key => $val) { // putting on global scope global $$key; $$key = $val; } return true; } /** * Merge UrlParsed to $_GET * * @param array $parsed parsed() array * @return bool true on success else false */ function putGet($parsed) { if (sizeof($parsed) == 0) { return false; } $_GET = array_merge($_GET,$parsed); return true; } /** * Merge UrlParsed to $_POST * * @param array $parsed parsed() array * @return bool true on success else false */ function putPost($parsed) { if (sizeof($parsed) == 0) { return false; } $_POST = array_merge($_POST,$parsed); return true; } /** * Merge UrlParsed to $_REQUEST * * @param array $parsed parsed() array * @return bool true on success else false */ function putRequest($parsed) { if (sizeof($parsed) == 0) { return false; } $_REQUEST = array_merge($_REQUEST,$parsed); return true; } /** * Merge UrlParsed to $_REQUEST, $_GET and $_POST * * @param array $parsed parsed() array * @return bool true on success else false */ function putAll($parsed) { if (sizeof($parsed) == 0) { return false; } UrlParser::putGet($parsed); UrlParser::putPost($parsed); UrlParser::putRequest($parsed); return true; } } ?> -- Atenciosamente, --------------------------------------------------------- Roberto Berto - roberto@desenvolve.com.br Gerente de Contas / Desenvolve Hospedagem a R$ 7,60 por mês, com toda a nossa qualidade! Confira: http://www.desenvolve.com.br DESENVOLVE Soluções de Internet http://www.desenvolve.com.br TEL/FAX: 51 32270599 Rua Duque de Caxias, 1594 conj. 204 CEP 90010-281 - Porto Alegre - RS ---------------------------------------------------------

Attachment: [application/x-php] UrlParser_test.php
« previous php.pear.dev (#12097) next »