Ver Mensaje Individual
  #14 (permalink)  
Antiguo 22/04/2010, 09:58
calamarjoe
 
Fecha de Ingreso: abril-2010
Mensajes: 10
Antigüedad: 16 años, 6 meses
Puntos: 0
Respuesta: Pierdo variables usando una clase dentro de otra

Finalmente me ayudó un colega a solucionarlo. En ningún momento usó la función preg_replace_callback() y funciona a la perfección.

Así ha quedado:
Código PHP:
<?php 

class Flickr { 

    private 
$apiKey = 'c933e0d66a6695d005d660392bc496f2'; 

    public function 
img($id) 
    { 
        
$url = 'http://www.flickr.com/services/rest/?api_key=' . $this->apiKey . '&format=php_serial&method=flickr.photos.getInfo&photo_id=' . $id; 

        
$resultado = file_get_contents($url); 
        
$resultado = unserialize($resultado); 
        
$foto = $resultado[photo]; 

        
$foto_url = 'http://farm' . $foto[farm] . '.static.flickr.com/' . $foto[server] . '/' . $foto[id] . '_' . $foto[secret] . $tam . '.jpg'; 

        
$foto = '<a href="' . $foto[urls][url][0][_content] . '"><img src="' . $foto_url . '" alt="' . $foto[title][_content] . '" /></a>'; 

        return 
$foto; 
    } 

} 

$flickr = new Flickr(); 



class 
BBcode { 

    function 
__construct(&$flickr) 
    { 
        
$this->flickr =& $flickr;  
    } 

    function 
procesar($texto){ 

        
$a = "/\[img\](.*?)\[\/img\]/is";  

        
preg_match_all($a, $texto, $matches);
        
        for(
$i = 0; $i < count($matches[0]); $i++) { // matches[0] -> la línea completa, matches[1] -> el grupo capturado
            
$real_deal = $this->flickr->img($matches[1][$i]);
            
$texto = str_replace($matches[0][$i], $real_deal, $texto); // replaceamos y listo
        
}
        
        return 
$texto; 
    } 
} 

$texto  = 'Imagen 1:<br />'; 
$texto .= '[img]4398563145[/img]<br />'; 
$texto .= 'Imagen 2:<br />'; 
$texto .= '[img]4334520973[/img]'; 

$bbcode = new BBcode($flickr); 

echo 
$bbcode->procesar($texto); 

?>