Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/03/2011, 15:06
Avatar de geeck22
geeck22
 
Fecha de Ingreso: agosto-2010
Ubicación: Tijuana B.C.
Mensajes: 79
Antigüedad: 13 años, 8 meses
Puntos: 7
duda de Funciones PHP y bbcode

Saludos!,
hoy me encentro haciendo un script para publicar post en foros como por ejemplo este foro,
que tenga funciones basicas como texto en negritas, subrayado o cursiva, texto centrado, justificado, inserccion de links, imagenes, videos de youtube, caritas y ahora quiero agregarle una opcion para poner codigo html o php o del que sea necesario, ademas quiero colorearlo para lo cual tengo el siguiente script:
Código PHP:
function ColoreaCodigo($texto) {
$texto = str_replace("\n", "", $texto);
$texto = str_replace("\n", "", $texto);
$texto = str_replace(">", ">", $texto);
$texto = str_replace("<", "<", $texto);
$texto = str_replace("&", "&", $texto);
$texto = str_replace('$', '\$', $texto);
$texto = str_replace('n', '\n', $texto);
$texto = str_replace('r', '\r', $texto);
$texto = str_replace('t', '\t', $texto);
$texto = str_replace('"', '"', $texto);
$texto = stripslashes($texto);
ob_start();
$nivelviejo=error_reporting(0);
highlight_string($texto);
error_reporting($nivelviejo);
$buffer = ob_get_contents();
ob_end_clean();
if ($variable) {
$abrirpost = strpos($buffer,'<?');
$cerrarpost = strrpos($buffer, '
?);
$buffer=substr($buffer0$abrirpost).substr($buffer$abrirpost+5$cerrarpost-($abrirpost+5)).substr($buffer$cerrarpost+5);
}
$buffer=str_replace("","",$buffer);
$buffer=str_replace("","",$buffer);
return 
$buffer;
}
y este es de mi funcion para el bbcode
Código PHP:
function bbcode($codigo)
{
$search array_keys$GLOBALS['bb_code'] );
$codigo str_replace$search$GLOBALS['bb_code'], $codigo );
$codlink eregi_replace("\\[link=([^\\[]*)\\]([^\\[]*)\\[/link\\]""<a href=\"\\1\" target=\"_blank\">\\1</a>"$codigo); 
$codlink eregi_replace("\\[link\\]([^\\[]*)\\[/link\\]","<a href=\"\\1\" target=\"_blank\">\\1</a>",$codlink);  
return 
$codlink;    

y al aplicar la funcion para colorear codigo a la de bbcode se ve algo asi:
Código PHP:
function bbcode($codigo)
{
$search = array_keys( $GLOBALS['bb_code'] );
$codigo = str_replace( $search, $GLOBALS['bb_code'], $codigo );
$codlink = eregi_replace("\\[link=([^\\[]*)\\]([^\\[]*)\\[/link\\]", "<a href=\"\\1\" target=\"_blank\">\\1</a>", $codigo); 
$codlink = eregi_replace("\\[link\\]([^\\[]*)\\[/link\\]","<a href=\"\\1\" target=\"_blank\">\\1</a>",$codlink);  
$texto = str_replace("\n", "", $codlink);
$texto = str_replace("\n", "", $texto);
$texto = str_replace(">", ">", $texto);
$texto = str_replace("<", "<", $texto);
$texto = str_replace("&", "&", $texto);
$texto = str_replace('$', '\$', $texto);
$texto = str_replace('n', '\n', $texto);
$texto = str_replace('r', '\r', $texto);
$texto = str_replace('t', '\t', $texto);
$texto = str_replace('"', '"', $texto);
$texto = stripslashes($texto);
ob_start();
$nivelviejo=error_reporting(0);
highlight_string($texto);
error_reporting($nivelviejo);
$buffer = ob_get_contents();
ob_end_clean();
if ($variable) {
$abrirpost = strpos($buffer,'<?');
$cerrarpost = strrpos($buffer, '
?);
$buffer=substr($buffer0$abrirpost).substr($buffer$abrirpost+5$cerrarpost-($abrirpost+5)).substr($buffer$cerrarpost+5);
}
$buffer=str_replace("","",$buffer);
$buffer=str_replace("","",$buffer);
return 
$buffer;
return 
$codlink;    
}
aqui es donde se presenta mi problema, cuando quiero aplicar la funcion por ejemplo asi:
Código PHP:
$post = 'Esta palabra está en [b]negrita[/b] y un codigo coloreado: [codigo]<html> o <?php echo "Hola!"?>[/codigo]';

echo '<p>'.bbcode($post).'</p>';
espero que me salga algo asi:
Esta palabra está en negrita y un codigo coloreado:
Código PHP:
<html> o <?php echo "Hola!"?>
pero por el contrario sale asi:
<span style="font-weight:bold">negrita</span> y un codigo coloreado[codigo]<html> ok pero wtf <?php echo "Hola!"; ?>[/codigo]

No encuentro la manera de que solo me transforme el codigo que este entre las etiquetas [codigo][/codigo] alguien tiene una idea de como puedo solucionarlo? :(