/trasweb/common/helpers/String.class.php
PHP | 141 lines | 59 code | 24 blank | 58 comment | 13 complexity | bfd5b2ba3a0117259e250fee068cf6ce MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php
- /**
- New Licence bsd:
- Copyright (c) <2012>, Manuel Jesus Canga Muñoz
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- * Neither the name of the Trasweb.net nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL Manuel Jesus Canga Muñoz BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- /** ***************************************************************************
- Esta clase sirve para el manejo de cadenas
- *************************************************************************** */
- class String
- {
- /**
- Convierte un string en un array desglosado del namespace
- @param String $_namespace String namespace que se desglosara.
- */
- function toNamespace($_namespace = NULL) {
- $info = array('module' => NULL, 'component' => NULL, "namespace" => '', "full_namespace" => $_namespace, "last" = NULL );
- //Si no se especifico un namespace, no hay nada que hacer
- if(empty($_namespace)) return $info;
-
- //Eliminamos la posible barra inclinada del principio del namespace
- if ( "\\" == $_namespace[0] ) {
- $namespace = substr($_namespace,1);
- }else {
- $namespace = $_namespace;
- }
- //Si despues de quitar la barra, vemos que no queda nada. Obvio que no podemos seguir.
- if(empty($namespace) ) return $info;
- //Troceamos el namespace
- $namespace_split = explode("\\", $namespace );
-
- //El primer elemento del namespace siempre sera un componente.
- $info["component"] = Filter::alphameric($namespace[0]);
-
- //Si el componente ha resultado no valido, no continuamos
- if(empty($info["component"]) ) {
- return $info;
- }
- //El segundo elemento del namespace, si es que existe, sera siempre un modulo
- if(count($namespace) == 2 ) {
- $info["module"] = Filter::alphameric($namespace[1]);
- //Si el modulo ha resultado no valido, invalidamos todo lo anterior y salimos
- if(empty($info["module"])) {
- $info["component"] = NULL;
- return $info;
- }
- $info["last"] = $info["module"];
- }else {
- $info["last"] = $info["component"];
- }
- $info["namespace"] = $namespace;
- return $info;
- }
- /**
- Cortamos una serie de carácteres de la cadena
- @param String $_string: La cadena que queremos cortar
- @param Int $_length: El numero de caracteres que permaneceran en la cadena original
- */
- function cut($_string, $_length = 1000) { return @substr(trim($_string), 0, $_length ); }
- /** Convert a string in a url */
- function convertUrl($_string, $otros_permitidos = null) {
- //Convertimos semiválidos a carácteres válidos
- $_antes = array("á", "é", "í", "ó", "ú", "ä","ë", "ï", "ö", "ü", "ñ", "&", " ", "_", "Á","É", "Í", "Ó", "Ú");
- $_despues = array("a", "e", "i", "o", "u", "a","e", "i", "o", "u", "n", "y", "-", "-", "a","e", "i", "o", "u");
- $_string = str_replace($_antes, $_despues, $_string);
- //quitamos aquellos carácteres que no sean alfanumericos o guion
- $_string = preg_replace("/^A-Za-z0-9-". $otros_permitidos."/", "", strtolower($_string) );
- //Quitamos todos aquellos guiones que se hayan quedado juntos. Ejemplo: mi---noticia -> mi-noticia
- $_string = preg_replace("/-{2,}/","-", $_string);
- //Quitamos todos aquellos guiones del principio y del final de la cadena. ej: ---prueba--- -> prueba
- $_string = preg_replace("/^-|-+$/","",$_string);
- //Devolvemos la url urlcodeada por si acaso queda algo raro
- return urlencode($_string);
- }
- /** Mi propio explode
- splitit(".", "archivo.pdf.tar.gz", 2) -> archivo.pdf | tar.gz
- splitit(".","archivo.pdf.zip.tar.gz", -2) -> archivo.pdf.zip | tar.gz
- splitit(".", "archivo.pdf.zip.tar.gz") -> archivo | pdf | zip | tar | gz ( explode ) */
- function splitit($_sep, $_string, $_limit = 0) {
-
- $array = explode($_sep, $_string);
- if($_limit == 0)
- return $array;
- else if($_limit >0) {
- $array_copy = $array;
- return Array(
- 0 => implode($_sep, array_splice($array, 0, $_limit)),
- 1 => implode($_sep, array_splice($array_copy, $_limit))
- );
- } else {
- $array_copy = $array;
- return Array(
- 0 => implode($_sep,array_splice($array, 0, $_limit)),
- 1 => implode($_sep,array_splice($array_copy, $_limit))
- );
- }
- }
- }