Re: New class purposal: UrlParser]
| From: | Roberto Bertó | Date: | Sun, 05 Jan 2003 09:24:52 +0000 |
| Subject: | Re: New class purposal: UrlParser] | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-12122@lists.php.net to get a copy of this message | ||
Since the purpose of the PEAR Packages is avoid these lines of code you
suggested, I guess its better add the methods I wrote to Net_URL, maybe
with some modifications.
I am contactting NET_URL leader to know what he think
On Sun, 2003-01-05 at 02:16, Jesus M. Castagnetto wrote:
> Hmm, AFAIK this is OK w/ the definitions of URLs, and it is alternative to
> using a query string to make a target URL more "search-engine friendly" (or
> just look nicer), but why are you reinventing some parts that are already in
> PEAR::Net_URL?
>
> For example, using your URL and Net_URL I can simply do:
>
> include 'Net/URL.php';
>
> $url = new
> Net_URL('http://example.com/path/to/my/name');
> $tmp = explode('/', $url->path);
> $n = count($tmp);
> for ($i=1; $i < $n; $i += 2) {
> $GLOBALS[$tmp[$i]] = $tmp[$i + 1];
> }
>
> and even w/o using Net_URL, but just parse_url()
>
> $url =
> parse_url('http://example.com/path/to/my/name');
> $tmp = $url['path'];
> ... etc ...
>
> One can even modify that so if a diff array will be used for the vars, the
> $GLOBALS ref can be replaced by the appropriate superglobal, or one can use:
>
> $vars =& $_POST;
>
> for assigning to the POST vars, and then do:
>
> $vars[$tmp[$i]] = $tmp[$i + 1];
>
> BTW, PEAR::Net_URL needs and update to use $_SERVER instead of the deprecated
> $HTTP_SERVER_VARS
>
> So I am not yet convinced about the need for this particular package that also
> does URL parsing, w/ a diff API for a non-general use.
>
>
> --- Roberto Bertó <roberto@desenvolve.com.br> wrote:
> > Hi all,
> >
> > I am turning a class I wrote to PEAR, since I guess there are lots of
> > guys that use something like it. I search at PEAR and didnt find
> > anything like this package.
> >
> > Its UrlParser, that, convert
> > http://example.com/path/to/my/name to
> > path=to and my=name
> >
> >
> > I am sending the code I am converting to PEAR format.
> >
> > I hope you approve it.
> >
> >
> >
> > <?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$
> >
> > require_once "PEAR.php";
> >
> > /**
> > * 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.
> > *
> > * If the number of subpaths isn't a pair, 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.
> > *
> > *
> > * METHODS:
> > * array parse([string URL])
> > * return an key=>val array with the URL or from PHP ENV
> > *
> > * void putGet(array)
> > * will merge the given UrlParser array with the $_GET var
> > *
> > * void putRequest(array)
> > * will merge the given UrlParser array with $_REQUEST var
> > *
> > * void putPost(array)
> > * merge the given UrlParser array with $_POST
> > *
> > * void putAll(array)
> > * merge given UrlParser array with $_GET, $_REQUEST and $_POST
> > *
> > * void 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
> > {
> > function parse($url = NULL)
> > {
> > // guessing current URL
> >
> > if (empty($url))
> > {
> > $url = $_SERVER["PATH_INFO"];
> > }
> >
> > // running defined config
> > if (CMS_MODULES_URLPARSER == TRUE)
> > {
> > $parsed = $this->parse($url);
> > }
> >
> > if (CMS_MODULES_URLPARSER_GET == TRUE)
> > {
> > $get = TRUE;
> > } else {
> > $get = FALSE;
> > }
> >
> > if (CMS_MODULES_URLPARSER_REQUEST == TRUE)
> > {
> > $request = TRUE;
> > } else {
> > $request = FALSE;
> > }
> >
> > $this->merge($parsed,$get,$request);
> >
> > if (CMS_MODULES_URLPARSER_SMARTY == TRUE)
> > {
> > $this->smarty($parsed);
> > }
> >
> >
> >
> > }
> >
> > /**
> > * URI Parse
> > *
> > * Parse a URL and return an associative array
> > *
> > * @param string $uri uri to turn into an
> > associative array
> > * @return array a key=>value array of the URL
> > */
> > function parse($uri)
> > {
> > // cant have a \ at the end
> > $uri = preg_replace("/\/$/","",$uri);
> >
> > $elements = explode("/",$uri);
> > for ($i = sizeof($elements) - 1; $i >= 0; $i--)
> > {
> > $x++;
> > if ($x % 2 == 0)
> > {
> > if (!empty($elements[$i]))
> > {
> > $parsed["{$elements[$i]}"] = $cache;
> > }
> > } else {
> > $cache = urldecode($elements[$i]);
> > }
> > }
> > return $parsed;
> > }
> >
> > /**
> > * Merge
> > *
> > * Merge the URI parsed into $_GET and/or/nor REQUEST
> > *
> > * @param string $uri uri to pass to $this->parse
> > * @param bool $get if true, merge _$GET with the
> > parsed
> > * @param bool $request if true, merge $_REQUEST with
> > the parsed
> > */
> > function merge($parsed,$get,$request)
> > {
> > if ($get == TRUE)
> > {
> > $_GET = array_merge($_GET,$parsed);
> > }
> >
> > if ($request == TRUE)
> > {
> > $_REQUEST = array_merge($_REQUEST,$parsed);
> > }
> > }
> >
> > function smarty($parsed)
> > {
> > if (sizeof($parsed) == 0) {
> > return FALSE;
> > }
> >
> > global $cms;
> > foreach ($parsed as $key => $val)
> > {
> > $cms->smarty->assign("url_" . $key,$val);
> > }
> > }
> > }
> >
> > ?>
> >
> >
> >
> > --
> >
> >
> >
> >
> > 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
> > ---------------------------------------------------------
> >
> >
> >
> > --
> > PEAR Development Mailing List (http://pear.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
>
> =====
> --- Jesus M. Castagnetto (jcastagnetto@yahoo.com)
>
> Research:
> http://metallo.scripps.edu/
> Personal: http://www.castagnetto.org/
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com
--
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
---------------------------------------------------------
--
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
---------------------------------------------------------