Foros del Web » Programando para Internet » PHP »

PHP OO Preg replace y variables

Estas en el tema de Preg replace y variables en el foro de PHP en Foros del Web. Código PHP:    $a  = array(        "/\[i\](.*?)\[\/i\]/is" ,        "/\[b\](.*?)\[\/b\]/is" ,        "/\[u\](.*?)\[\/u\]/is" ,        "/\[img\](.*?)\[\/img\]/is"     );     $b  = array(        "<i>$1</i>" ,        "<b>$1</b>" ...
  #1 (permalink)  
Antiguo 30/03/2011, 13:04
 
Fecha de Ingreso: septiembre-2009
Mensajes: 230
Antigüedad: 14 años, 7 meses
Puntos: 2
Preg replace y variables

Código PHP:
   $a = array(
      
"/\[i\](.*?)\[\/i\]/is",
      
"/\[b\](.*?)\[\/b\]/is",
      
"/\[u\](.*?)\[\/u\]/is",
      
"/\[img\](.*?)\[\/img\]/is"
   
);
   
$b = array(
      
"<i>$1</i>",
      
"<b>$1</b>",
      
"<u>$1</u>",
      
"<img src=\"$1\" style=\"width:".img_size("$1","width").";height:".img_size("$1","height").";margin:auto;\" />"
   
);
    
$texto preg_replace($a$b$texto);
return 
$texto
La parte de la imagen, img_size es una función, $1 tendría que ser el mismo src que al principio, quiero decir, tendría uqe haber un link, sin embargo, me larga este error:

Cita:
Warning: getimagesize($1) [function.getimagesize]: failed to open stream: No such file or directory in C:\AppServ\www\CulturaBahiense\m\sys\cfg\functions .php on line 319

Warning: getimagesize($1) [function.getimagesize]: failed to open stream: No such file or directory in C:\AppServ\www\CulturaBahiense\m\sys\cfg\functions .php on line 319
Ayuda?
  #2 (permalink)  
Antiguo 30/03/2011, 14:01
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Preg replace y variables

usa preg_replace_callback(), o bien el modificador de evaluación en la expresión...

la cadena que te esta dando error es simplemente porque se evalúa al momento de definirla, porque al fin y al cabo se trata de php activo.... si la idea es evaluar dicha frase después, ya con el reemplazo, pues has lo que te sugiero...
Código PHP:
// con modificador
echo preg_replace('/x/e''"string" . strtoupper("\\0") . "other_string"''xyz');

// con callback
echo preg_replace_callback('/x/', function($matches) {
  return 
"string" strtoupper($matches[0]) . "other_string";
}, 
'xyz'); 
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #3 (permalink)  
Antiguo 30/03/2011, 17:27
 
Fecha de Ingreso: septiembre-2009
Mensajes: 230
Antigüedad: 14 años, 7 meses
Puntos: 2
Respuesta: Preg replace y variables

Cita:
Iniciado por pateketrueke Ver Mensaje
usa preg_replace_callback(), o bien el modificador de evaluación en la expresión...

la cadena que te esta dando error es simplemente porque se evalúa al momento de definirla, porque al fin y al cabo se trata de php activo.... si la idea es evaluar dicha frase después, ya con el reemplazo, pues has lo que te sugiero...
Código PHP:
// con modificador
echo preg_replace('/x/e''"string" . strtoupper("\\0") . "other_string"''xyz');

// con callback
echo preg_replace_callback('/x/', function($matches) {
  return 
"string" strtoupper($matches[0]) . "other_string";
}, 
'xyz'); 

no entiendo lo del callback, ya busque al funcion en php.net pero sigo sin entender, eso me devuelve $1? No comprendo!
  #4 (permalink)  
Antiguo 31/03/2011, 17:18
 
Fecha de Ingreso: septiembre-2009
Mensajes: 230
Antigüedad: 14 años, 7 meses
Puntos: 2
Respuesta: Preg replace y variables

Código PHP:
<? 
function img_size($url,$type) {

    
// Empiezo el proceso para archivar el tamaño y dimensiones de la imágen
    
list($width$height) = getimagesize($url);
    if(
$type == 'width') {
    if(
$width '504'$width '504';
    elseif(
$width <= '504'$width $width;
    return 
$width;
    }
    if(
$type == 'height') {
    if(
$height '378'$height '378';
    elseif(
$height <= '378'$height $height;
    return 
$height;
    }
    
}

   
$a = array(
      
"/\[i\](.*?)\[\/i\]/is",
      
"/\[b\](.*?)\[\/b\]/is",
      
"/\[u\](.*?)\[\/u\]/is",
      
"/\[center\](.*?)\[\/center\]/is",
      
"/\[left\](.*?)\[\/left\]/is",
      
"/\[right\](.*?)\[\/right\]/is",
      
"/\[img\](.*?)\[\/img\]/is",
      
"/\[url=(.*?)\](.*?)\[\/url\]/is",
      
"/\[url](.*?)\[\/url\]/is",
      
"/\[quote=(.*?)\](.*?)\[\/quote\]/is",
      
"/\[quote](.*?)\[\/quote\]/is",
      
"/\[youtube](.*?)\[\/youtube\]/is",
      
"/\[vimeo](.*?)\[\/vimeo\]/is"
   
);
   
$b = array(
      
"<i>$1</i>",
      
"<b>$1</b>",
      
"<u>$1</u>",
      
"<center>$1</center>",
      
"<left>$1</left>",
      
"<right>$1</right>",
      
"<img src=\"$1\" style=\"width:".img_size(strtoupper("\\1"),"width").";height:".img_size(strtoupper("\\1"),"height").";margin:auto;\" />",
      
"<a href=\"$1\">$2</a>",
      
"<a href=\"$1\">$1</a>",
      
"<div class=\"quote\">Cita:<br/> <div class=\"bgquote\">Empezado por: <b>$1</b><br/> <font style=\"font-size:10px;\"><i>$2</i></font></div></div>",
      
"<div class=\"quote\">Cita:<br/> <div class=\"bgquote\">Empezado por alguién:<br/> <font style=\"font-size:10px;\"><i>$1</i></font></div></div>",
      
"<object style=\"height: 390px; width: 640px\">
<param name=\"movie\" value=\"http://www.youtube.com/v/$1?version=3\">
<param name=\"allowFullScreen\" value=\"true\">
<param name=\"allowScriptAccess\" value=\"always\">
<embed src=\"http://www.youtube.com/v/$1?version=3\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowScriptAccess=\"always\" width=\"640\" height=\"390\"></object>"
,
      
"<object style=\"height: 300px; width: 400px\">
<param name=\"movie\" value=\"http://player.vimeo.com/video/$1\">
<param name=\"allowFullScreen\" value=\"true\">
<param name=\"allowScriptAccess\" value=\"always\">
<embed src=\"http://player.vimeo.com/video/$1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowScriptAccess=\"always\" width=\"400\" height=\"300\"></object>"
   
);

// con modificador
echo preg_replace($a$b'[img]http://a3.sphotos.ak.fbcdn.net/hphotos-ak-ash4/184727_1885408622011_1445276233_32089958_459342_n.jpg[/img]');
?>
Sigo teniendo errores:

La imagen la muestra, pero arriba lista este error:

Cita:
Warning: getimagesize(\1) [function.getimagesize]: failed to open stream: No such file or directory in C:\AppServ\www\CulturaBahiense\prueba.php on line 5

Warning: getimagesize(\1) [function.getimagesize]: failed to open stream: No such file or directory in C:\AppServ\www\CulturaBahiense\prueba.php on line 5
  #5 (permalink)  
Antiguo 31/03/2011, 17:39
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Preg replace y variables


Código PHP:
echo preg_replace('/\[img\](.+?)\[\/img\]/e''"<img src=\"\\1\" width=\"" . img_size("\\1", "width") . "\" height=\"" . img_size("\\1", "height") . "\"/>"'$texto); 
Edito: olvidé el modificador de evaluación...
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.

Última edición por pateketrueke; 31/03/2011 a las 18:00 Razón: /e
  #6 (permalink)  
Antiguo 31/03/2011, 17:46
 
Fecha de Ingreso: septiembre-2009
Mensajes: 230
Antigüedad: 14 años, 7 meses
Puntos: 2
Respuesta: Preg replace y variables

Código PHP:
<? 
function img_size($url,$type) {

    
// Empiezo el proceso para archivar el tamaño y dimensiones de la imágen
    
list($width$height) = getimagesize($url);
    if(
$type == 'width') {
    if(
$width '504'$width '504';
    elseif(
$width <= '504'$width $width;
    return 
$width;
    }
    if(
$type == 'height') {
    if(
$height '378'$height '378';
    elseif(
$height <= '378'$height $height;
    return 
$height;
    }
    
}

   
$a = array(
      
"/\[i\](.*?)\[\/i\]/is",
      
"/\[b\](.*?)\[\/b\]/is",
      
"/\[u\](.*?)\[\/u\]/is",
      
"/\[center\](.*?)\[\/center\]/is",
      
"/\[left\](.*?)\[\/left\]/is",
      
"/\[right\](.*?)\[\/right\]/is",
      
"/\[img\](.*?)\[\/img\]/is",
      
"/\[url=(.*?)\](.*?)\[\/url\]/is",
      
"/\[url](.*?)\[\/url\]/is",
      
"/\[quote=(.*?)\](.*?)\[\/quote\]/is",
      
"/\[quote](.*?)\[\/quote\]/is",
      
"/\[youtube](.*?)\[\/youtube\]/is",
      
"/\[vimeo](.*?)\[\/vimeo\]/is"
   
);
   
$b = array(
      
"<i>$1</i>",
      
"<b>$1</b>",
      
"<u>$1</u>",
      
"<center>$1</center>",
      
"<left>$1</left>",
      
"<right>$1</right>",
      
'<img src=\"\\1\" width=\"" . img_size("\\1", "width") . "\" height=\"" . img_size("\\1", "height") . "\"/>',
      
"<a href=\"$1\">$2</a>",
      
"<a href=\"$1\">$1</a>",
      
"<div class=\"quote\">Cita:<br/> <div class=\"bgquote\">Empezado por: <b>$1</b><br/> <font style=\"font-size:10px;\"><i>$2</i></font></div></div>",
      
"<div class=\"quote\">Cita:<br/> <div class=\"bgquote\">Empezado por alguién:<br/> <font style=\"font-size:10px;\"><i>$1</i></font></div></div>",
      
"<object style=\"height: 390px; width: 640px\">
<param name=\"movie\" value=\"http://www.youtube.com/v/$1?version=3\">
<param name=\"allowFullScreen\" value=\"true\">
<param name=\"allowScriptAccess\" value=\"always\">
<embed src=\"http://www.youtube.com/v/$1?version=3\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowScriptAccess=\"always\" width=\"640\" height=\"390\"></object>"
,
      
"<object style=\"height: 300px; width: 400px\">
<param name=\"movie\" value=\"http://player.vimeo.com/video/$1\">
<param name=\"allowFullScreen\" value=\"true\">
<param name=\"allowScriptAccess\" value=\"always\">
<embed src=\"http://player.vimeo.com/video/$1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowScriptAccess=\"always\" width=\"400\" height=\"300\"></object>"
   
);

// con modificador
echo preg_replace($a$b'[img]http://a3.sphotos.ak.fbcdn.net/hphotos-ak-ash4/184727_1885408622011_1445276233_32089958_459342_n.jpg[/img]');

?>

Codigo fuente de la plantilla
Cita:
<img src=\"http://a3.sphotos.ak.fbcdn.net/hphotos-ak-ash4/184727_1885408622011_1445276233_32089958_459342_n. jpg\" width=\"" . img_size("http://a3.sphotos.ak.fbcdn.net/hphotos-ak-ash4/184727_1885408622011_1445276233_32089958_459342_n. jpg", "width") . "\" height=\"" . img_size("http://a3.sphotos.ak.fbcdn.net/hphotos-ak-ash4/184727_1885408622011_1445276233_32089958_459342_n. jpg", "height") . "\"/>

Etiquetas: preg, replace, variables
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 23:04.