Skip to content

Instantly share code, notes, and snippets.

@albertein
Created January 26, 2012 16:42
Show Gist options
  • Save albertein/1683702 to your computer and use it in GitHub Desktop.
Save albertein/1683702 to your computer and use it in GitHub Desktop.
Apply nl2br to a string except for the content of a given string (Useful for <pre> tags)
<?
function smart_nl2br($data, $opening_tag, $closing_tag) {
$results = "";
$closing_tag_length = strlen($closing_tag);
while(($position_start = strpos($data, $opening_tag)) !== false) {
$position_end = strpos($data, $closing_tag);
$previous_data = substr($data, 0, $position_start);
$tag_data = substr($data, $position_start, $position_end - $position_start + $closing_tag_length);
$data = substr($data, $position_end + $closing_tag_length);
$results = $results . nl2br($previous_data) . $tag_data;
}
return $results;
}
//usage: $results = smart_nl2br("Some string", "[code]", "[/code]");
//Testing code:
$data = "
Lorem
Impsum
Dicto
Sit
[b]- Incluimos las librerias jquery y ajaxupload[/b]
[code]<script type=\"text/javascript\" src=\"jquery.js\"></script>
<script type=\"text/javascript\" src=\"ajaxupload.js\"></script></pre>
<p><strong>- Html</strong></p>
<pre class=\"brush:xml\"><div id=\"content\">
<a href=\"javascript:;\" id=\"upload\">Subir Foto</a>
<ul id=\"gallery\">
<!-- Cargar Fotos -->
</ul>
</div>[/code]
[b]- Css[/b]
[code]<style type=\"text/css\">
body{
margin:0;
padding:0;
font:normal 12px Arial, Helvetica, sans-serif
}
#content{
width:700px;
margin:20px auto;
height:550px;
border:6px solid #F3F3F3;
padding-top:10px;
overflow-y:auto
}
#upload{
padding:12px;
font:bold 12px Arial, Helvetica, sans-serif;
text-align:center;
background:#f2f2f2;
color:#3366cc;
border:1px solid #ccc;
width:150px;
display:block;
-moz-border-radius:5px;
-webkit-border-radius:5px;
margin:0 auto;
text-decoration:none
}
#gallery{
list-style:none;
margin:20px 0 0 0;
padding:0
}
#gallery li{
display:block;
float:left;
width:155px;
height:160px;
background:#9AF099;
border:1px solid #093;
text-align:center;
padding:6px 0;
margin:5px 0 5px 14px
}
#gallery li img{
width:145px;
height:140px
}
</style>[/code]
- El javascript donde creamos una instancia de la clase Ajaxupload para poder cargar nuestras imagenes.
[code]<script type=\"text/javascript\">
$(document).ready(function(){
var button = $('#upload'), interval;
new AjaxUpload(button,{
action: 'procesa.php',
name: 'image',
onSubmit : function(file, ext){
// cambiar el texto del boton cuando se selecicione la imagen
button.text('Subiendo');
// desabilitar el boton
this.disable();
interval = window.setInterval(function(){
var text = button.text();
if (text.length < 11){
button.text(text + '.');
} else {
button.text('Subiendo');
}
}, 200);
},
onComplete: function(file, response){
button.text('Subir Foto');
window.clearInterval(interval);
// Habilitar boton otra vez
this.enable();
// Añadiendo las imagenes a mi lista
if($('#gallery li').length == 0){
$('#gallery').html(response).fadeIn(\"fast\");
$('#gallery li').eq(0).hide().show(\"slow\");
}else{
$('#gallery').prepend(response);
$('#gallery li').eq(0).hide().show(\"slow\");
}
}
});
// Listar fotos que hay en mi tabla
$(\"#gallery\").load(\"procesa.php?action=listFotos\");
});
</script>[/code]";
echo smart_nl2br($data, "[code]", "[/code]");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment