Foros del Web » Programando para Internet » PHP »

Eregi VS preg_match

Estas en el tema de Eregi VS preg_match en el foro de PHP en Foros del Web. Buenas, yo usaba este código para recoger el navegador que usaba un usuario: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código php: Ver original <?php   function ObtenerNavegador ( $user_agent ...
  #1 (permalink)  
Antiguo 24/07/2009, 20:14
 
Fecha de Ingreso: julio-2009
Mensajes: 30
Antigüedad: 14 años, 9 meses
Puntos: 0
Eregi VS preg_match

Buenas, yo usaba este código para recoger el navegador que usaba un usuario:
Código php:
Ver original
  1. <?php
  2.  
  3. function ObtenerNavegador($user_agent) {
  4.      $navegadores = array(
  5.           'Opera' => 'Opera',
  6.           'Mozilla Firefox'=> '(Firebird)|(Firefox)',
  7.           'Galeon' => 'Galeon',
  8.           'Mozilla'=>'Gecko',
  9.           'MyIE'=>'MyIE',
  10.           'Lynx' => 'Lynx',
  11.           'Netscape' => '(Mozilla/4\.75)|(Netscape6)|(Mozilla/4\.08)|(Mozilla/4\.5)|(Mozilla/4\.6)|(Mozilla/4\.79)',
  12.           'Konqueror'=>'Konqueror',
  13.           'Internet Explorer 7' => '(MSIE 7\.[0-9]+)',
  14.           'Internet Explorer 6' => '(MSIE 6\.[0-9]+)',
  15.           'Internet Explorer 5' => '(MSIE 5\.[0-9]+)',
  16.           'Internet Explorer 4' => '(MSIE 4\.[0-9]+)',
  17. );
  18. foreach($navegadores as $navegador=>$pattern){
  19.        if(eregi($pattern, $user_agent))
  20.        return $navegador;
  21.  
  22. }
  23. return "desconocido";
  24. }
  25.  
  26. echo ObtenerNavegador($_SERVER['HTTP_USER_AGENT']);
  27. ?>

Sin embargo, eregi ha sido "deprecated" en la versión 5.3 y de eliminará en la versión 6, el sustituto se supone que es preg_match, pero al cambiarlo me aparecen estos errores...

Cita:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\navegador.php on line 19

Warning: preg_match() [function.preg-match]: Unknown modifier '|' in C:\wamp\www\navegador.php on line 19

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\navegador.php on line 19

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\navegador.php on line 19

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\navegador.php on line 19

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\navegador.php on line 19

Warning: preg_match() [function.preg-match]: Unknown modifier '|' in C:\wamp\www\navegador.php on line 19

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\navegador.php on line 19
desconocido
Creo que los errores son por las expresiones regulares, pero no le encuentro sentido y estoy algo atascado.

Vale, arreglado, las expresiones regulares todas tenían que estar encerredas entre (..) tal que así:
Código php:
Ver original
  1. <?php
  2.  
  3. function ObtenerNavegador($user_agent){
  4.  
  5.     $navegadores = array (
  6.         'Opera' => '(Opera)',
  7.         'Mozilla Firefox'=> '((Firebird)|(Firefox))',
  8.         'Galeon' => '(Galeon)',
  9.         'Mozilla'=>'(Gecko)',
  10.         'MyIE'=>'(MyIE)',
  11.         'Lynx' => '(Lynx)',
  12.         'Netscape' => '((Mozilla/4\.75)|(Netscape6)|(Mozilla/4\.08)|(Mozilla/4\.5)|(Mozilla/4\.6)|(Mozilla/4\.79))',
  13.         'Konqueror'=>'(Konqueror)',
  14.         'Internet Explorer 7' => '(MSIE 7\.[0-9]+)',
  15.         'Internet Explorer 6' => '(MSIE 6\.[0-9]+)',
  16.         'Internet Explorer 5' => '(MSIE 5\.[0-9]+)',
  17.         'Internet Explorer 4' => '(MSIE 4\.[0-9]+)',
  18.     );
  19.    
  20.     foreach($navegadores as $navegador=>$pattern){
  21.         if(preg_match($pattern, $user_agent))
  22.         return $navegador;
  23.     }
  24.    
  25.     return "desconocido";
  26. }
  27.  
  28. echo ObtenerNavegador($_SERVER['HTTP_USER_AGENT']);
  29. ?>

Última edición por Maracanaido; 24/07/2009 a las 20:22
  #2 (permalink)  
Antiguo 24/07/2009, 23:47
Avatar de Ronruby  
Fecha de Ingreso: julio-2008
Ubicación: 18°30'N, 69°59'W
Mensajes: 4.879
Antigüedad: 15 años, 9 meses
Puntos: 416
Respuesta: Eregi VS preg_match

preg_match() necesita delimitadores.

Código php:
Ver original
  1. if(preg_match("/$pattern/i", $user_agent))
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 14:13.