PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/trasweb/common/helpers/String.class.php

https://bitbucket.org/trasweb/trasweb-framework
PHP | 141 lines | 59 code | 24 blank | 58 comment | 13 complexity | bfd5b2ba3a0117259e250fee068cf6ce MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. New Licence bsd:
  4. Copyright (c) <2012>, Manuel Jesus Canga Muñoz
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of the Trasweb.net nor the
  14. names of its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL Manuel Jesus Canga Muñoz BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /** ***************************************************************************
  28. Esta clase sirve para el manejo de cadenas
  29. *************************************************************************** */
  30. class String
  31. {
  32. /**
  33. Convierte un string en un array desglosado del namespace
  34. @param String $_namespace String namespace que se desglosara.
  35. */
  36. function toNamespace($_namespace = NULL) {
  37. $info = array('module' => NULL, 'component' => NULL, "namespace" => '', "full_namespace" => $_namespace, "last" = NULL );
  38. //Si no se especifico un namespace, no hay nada que hacer
  39. if(empty($_namespace)) return $info;
  40. //Eliminamos la posible barra inclinada del principio del namespace
  41. if ( "\\" == $_namespace[0] ) {
  42. $namespace = substr($_namespace,1);
  43. }else {
  44. $namespace = $_namespace;
  45. }
  46. //Si despues de quitar la barra, vemos que no queda nada. Obvio que no podemos seguir.
  47. if(empty($namespace) ) return $info;
  48. //Troceamos el namespace
  49. $namespace_split = explode("\\", $namespace );
  50. //El primer elemento del namespace siempre sera un componente.
  51. $info["component"] = Filter::alphameric($namespace[0]);
  52. //Si el componente ha resultado no valido, no continuamos
  53. if(empty($info["component"]) ) {
  54. return $info;
  55. }
  56. //El segundo elemento del namespace, si es que existe, sera siempre un modulo
  57. if(count($namespace) == 2 ) {
  58. $info["module"] = Filter::alphameric($namespace[1]);
  59. //Si el modulo ha resultado no valido, invalidamos todo lo anterior y salimos
  60. if(empty($info["module"])) {
  61. $info["component"] = NULL;
  62. return $info;
  63. }
  64. $info["last"] = $info["module"];
  65. }else {
  66. $info["last"] = $info["component"];
  67. }
  68. $info["namespace"] = $namespace;
  69. return $info;
  70. }
  71. /**
  72. Cortamos una serie de carácteres de la cadena
  73. @param String $_string: La cadena que queremos cortar
  74. @param Int $_length: El numero de caracteres que permaneceran en la cadena original
  75. */
  76. function cut($_string, $_length = 1000) { return @substr(trim($_string), 0, $_length ); }
  77. /** Convert a string in a url */
  78. function convertUrl($_string, $otros_permitidos = null) {
  79. //Convertimos semiválidos a carácteres válidos
  80. $_antes = array("á", "é", "í", "ó", "ú", "ä","ë", "ï", "ö", "ü", "ñ", "&", " ", "_", "Á","É", "Í", "Ó", "Ú");
  81. $_despues = array("a", "e", "i", "o", "u", "a","e", "i", "o", "u", "n", "y", "-", "-", "a","e", "i", "o", "u");
  82. $_string = str_replace($_antes, $_despues, $_string);
  83. //quitamos aquellos carácteres que no sean alfanumericos o guion
  84. $_string = preg_replace("/^A-Za-z0-9-". $otros_permitidos."/", "", strtolower($_string) );
  85. //Quitamos todos aquellos guiones que se hayan quedado juntos. Ejemplo: mi---noticia -> mi-noticia
  86. $_string = preg_replace("/-{2,}/","-", $_string);
  87. //Quitamos todos aquellos guiones del principio y del final de la cadena. ej: ---prueba--- -> prueba
  88. $_string = preg_replace("/^-|-+$/","",$_string);
  89. //Devolvemos la url urlcodeada por si acaso queda algo raro
  90. return urlencode($_string);
  91. }
  92. /** Mi propio explode
  93. splitit(".", "archivo.pdf.tar.gz", 2) -> archivo.pdf | tar.gz
  94. splitit(".","archivo.pdf.zip.tar.gz", -2) -> archivo.pdf.zip | tar.gz
  95. splitit(".", "archivo.pdf.zip.tar.gz") -> archivo | pdf | zip | tar | gz ( explode ) */
  96. function splitit($_sep, $_string, $_limit = 0) {
  97. $array = explode($_sep, $_string);
  98. if($_limit == 0)
  99. return $array;
  100. else if($_limit >0) {
  101. $array_copy = $array;
  102. return Array(
  103. 0 => implode($_sep, array_splice($array, 0, $_limit)),
  104. 1 => implode($_sep, array_splice($array_copy, $_limit))
  105. );
  106. } else {
  107. $array_copy = $array;
  108. return Array(
  109. 0 => implode($_sep,array_splice($array, 0, $_limit)),
  110. 1 => implode($_sep,array_splice($array_copy, $_limit))
  111. );
  112. }
  113. }
  114. }