Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Cambiar texto a lowercase usando expresiones regulares.

Estas en el tema de Cambiar texto a lowercase usando expresiones regulares. en el foro de PHP en Foros del Web. Buenos dias, Tengo un problema con una lógica y no he podido solucionarlo, digamos que tengo un string del siguiente tipo. JXpHzkCrdoXD6R59gHqP3%2Fdz8bDzB8aNpze0w6QCE7Q%2F16 jtZ%2F1fx7zHmtQG1UVv%2BI4NDVnygp7FhCfVSAPBsQ%3D%3D Y tengo ...
  #1 (permalink)  
Antiguo 17/10/2014, 09:20
 
Fecha de Ingreso: junio-2008
Ubicación: Armenia- Colombia || El tigre - Venezuela
Mensajes: 34
Antigüedad: 15 años, 9 meses
Puntos: 1
Pregunta Cambiar texto a lowercase usando expresiones regulares.

Buenos dias,

Tengo un problema con una lógica y no he podido solucionarlo, digamos que tengo un string del siguiente tipo.

JXpHzkCrdoXD6R59gHqP3%2Fdz8bDzB8aNpze0w6QCE7Q%2F16 jtZ%2F1fx7zHmtQG1UVv%2BI4NDVnygp7FhCfVSAPBsQ%3D%3D

Y tengo la siguiente expresion regular.

/%(\d[A-F]|[A-F]\d)/

Lo que hace es buscar buscar el signo de porcentaje, seguido de un valor hexadecimal de dos caracters, asi que arriba seleccionaria asi:

JXpHzkCrdoXD6R59gHqP3%2Fdz8bDzB8aNpze0w6QCE7Q%2F16jtZ%2F1fx7zHmtQG1UVv%2BI4NDVnygp7FhCfVSAPBsQ%3D%3D

Necesito convertir esas selecciones a lowercase. Pero aun no encuentro como hacerlo, actualmente mi función luce asi:

Código PHP:
private function preg_replace_matches(array $matches){
    return 
strtolower($matches[0]);
}

private function 
encode_urlvariables($urlvariable){
        
$first_encodeurlencode($urlvariable);
        
$encoded_variablepreg_replace_callback('/%(\d[A-F]|[A-F]\d)/''preg_replace_matches'$first_encode);
        return 
$encoded_variable

Muchas gracias
  #2 (permalink)  
Antiguo 17/10/2014, 10:56
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 11 años, 10 meses
Puntos: 320
Respuesta: Cambiar texto a lowercase usando expresiones regulares.

Código PHP:
Ver original
  1. <?php
  2. $txt = 'JXpHzkCrdoXD6R59gHqP3%2Fdz8bDzB8aNpze0w6QCE7Q%2F16jtZ%2F1fx7zHmtQG1UVv%2BI4NDVnygp7FhCfVSAPBsQ%3D%3D';
  3.     $partes = array_filter(array_map(function($ele){ return strtolower(substr($ele, 0, 2)); }, explode("%", $txt)), "ctype_xdigit");
  4.     var_dump($partes);

Salida:
Cita:
array (size=6)
1 => string '2f' (length=2)
2 => string '2f' (length=2)
3 => string '2f' (length=2)
4 => string '2b' (length=2)
5 => string '3d' (length=2)
6 => string '3d' (length=2)
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios
  #3 (permalink)  
Antiguo 17/10/2014, 13:30
 
Fecha de Ingreso: junio-2008
Ubicación: Armenia- Colombia || El tigre - Venezuela
Mensajes: 34
Antigüedad: 15 años, 9 meses
Puntos: 1
Respuesta: Cambiar texto a lowercase usando expresiones regulares.

Lo acabo de completar, puede no ser la mejor opcion o el mejor codigo, pero funciona para lo que lo necesito.
Código PHP:
function lowercase_hex($string){
    
$first_encodeurlencode($string);
    
$subject $first_encode;
    
$pattern =  '/%(\d[A-F]|[A-F]\d)/';
    
preg_match_all($pattern$subject$matches);
    
$replace $matches['0'];
    
$replacer = array(); 
    foreach (
$replace as $key => $value) {
        
$replacer[$key] = strtolower($value); 
    }
    
$returns str_replace($replace$replacer$first_encode);
    return 
$returns;


Etiquetas: expresiones, usando, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:37.