elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.
 
Inicio Ayuda Buscar Ingresar Registrarse
28 Mayo 2012, 13:16  


Tema destacado: [AIO elhacker.NET] Compilación herramientas análisis y desinfección malware

+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP
| | | |-+  Duda: Crear una pila contando palabras encontradas en un string
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Duda: Crear una pila contando palabras encontradas en un string  (Leído 506 veces)
Darkkz

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Duda: Crear una pila contando palabras encontradas en un string
« en: 10 Septiembre 2011, 16:33 »

Hola, he utilizado el buscador y no he encontrado mi duda.

Estoy tratando de hacer a mano una comprobación de tags de BBCode, que compruebe que las etiquetas abiertas de cada tipo estén luego cerradas.
He intentado utilizar tanto eregi() como preg_match(), pero no consigo utilizarlas correctamente. Según he oído eregi() está deprecated.

Sería algo así:

Código
function str_postea($cadena1,$cadena2,$texto) {
$contador=0;
while(preg_match('/'.$cadena1.'/',$texto))
$contador++;
while(preg_match('/'.$cadena2.'/',$texto))
$contador--;
while($contador>0) {
$texto = $texto.$cadena2;
$contador--;
}
while($contador<0) {
$texto = str_replace($cadena2,"",$texto);
$contador++;
}
 
return $texto;
}
Código
$texto = str_postea("[b]","[/b]",$texto);

El problema es que los primeros while de la función son infinitos, pues si existe la cadena, existe siempre. ¿Alguna idea de cómo podría arreglarlo?

Gracias.


En línea
madpitbull_99
Moderador Global
***
Desconectado Desconectado

Mensajes: 1.898



Ver Perfil WWW
Re: Duda: Crear una pila contando palabras encontradas en un string
« Respuesta #1 en: 10 Septiembre 2011, 19:06 »

Mira si esto te sirve, es el BBCode Parser que viene con el editor Markitup.

Código
 
<?php
// ----------------------------------------------------------------------------
// markItUp! BBCode Parser
// v 1.0.6
// Dual licensed under the MIT and GPL licenses.
// ----------------------------------------------------------------------------
// Copyright (C) 2009 Jay Salvat
// http://www.jaysalvat.com/
// http://markitup.jaysalvat.com/
// ----------------------------------------------------------------------------
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ----------------------------------------------------------------------------
// Thanks to Arialdo Martini, Mustafa Dindar for feedbacks.
// ----------------------------------------------------------------------------
 
define ("EMOTICONS_DIR", Site::get_url('habari') . "/user/plugins/markup/markitup/parsers/bbcode/images/");
 
function BBCode2Html($text) {
       $text = trim($text);
 
       // BBCode [code]
       if (!function_exists('escape')) {
               function escape($s) {
                       global $text;
                       $text = strip_tags($text);
                       $code = $s[1];
                       $code = htmlspecialchars($code);
                       $code = str_replace("[", "&#38;#91;", $code);
                       $code = str_replace("]", "&#38;#93;", $code);
                       return '<pre><code>'.$code.'</code></pre>';
               }      
       }
       $text = preg_replace_callback('/\[code\](.*?)\[\/code\]/ms', "escape", $text);
 
       // Smileys to find...
       $in = array(     ':)',
                                        ':D',
                                        ':o',
                                        ':p',
                                        ':(',
                                        ';)'
       );
       // And replace them by...
       $out = array(    '<img alt=":)" src="'.EMOTICONS_DIR.'emoticon-happy.png" />',
                                        '<img alt=":D" src="'.EMOTICONS_DIR.'emoticon-smile.png" />',
                                        '<img alt=":o" src="'.EMOTICONS_DIR.'emoticon-surprised.png" />',
                                        '<img alt=":p" src="'.EMOTICONS_DIR.'emoticon-tongue.png" />',
                                        '<img alt=":(" src="'.EMOTICONS_DIR.'emoticon-unhappy.png" />',
                                        '<img alt=";)" src="'.EMOTICONS_DIR.'emoticon-wink.png" />'
       );
       $text = str_replace($in, $out, $text);
 
       // BBCode to find...
       $in = array(     '/\[b\](.*?)\[\/b\]/ms',      
                                        '/\[i\](.*?)\[\/i\]/ms',
                                        '/\[u\](.*?)\[\/u\]/ms',
                                        '/\[img\](.*?)\[\/img\]/ms',
                                        '/\[email\](.*?)\[\/email\]/ms',
                                        '/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms',
                                        '/\[size\="?(.*?)"?\](.*?)\[\/size\]/ms',
                                        '/\[color\="?(.*?)"?\](.*?)\[\/color\]/ms',
                                        '/\[quote](.*?)\[\/quote\]/ms',
                                        '/\[list\=(.*?)\](.*?)\[\/list\]/ms',
                                        '/\[list\](.*?)\[\/list\]/ms',
                                        '/\[\*\]\s?(.*?)\n/ms'
       );
       // And replace them by...
       $out = array(    '<strong>\1</strong>',
                                        '<em>\1</em>',
                                        '<u>\1</u>',
                                        '<img src="\1" alt="\1" />',
                                        '<a href="mailto:\1">\1</a>',
                                        '<a href="\1">\2</a>',
                                        '<span style="font-size:\1%">\2</span>',
                                        '<span style="color:\1">\2</span>',
                                        '<blockquote>\1</blockquote>',
                                        '<ol start="\1">\2</ol>',
                                        '<ul>\1</ul>',
                                        '<li>\1</li>'
       );
       $text = preg_replace($in, $out, $text);
 
       // paragraphs
       $text = str_replace("\r", "", $text);
       $text = "<p>".preg_replace("/(\n){2,}/", "</p><p>", $text)."</p>";
       $text = nl2br($text);
 
       // clean some tags to remain strict
       // not very elegant, but it works. No time to do better ;)
       if (!function_exists('removeBr')) {
               function removeBr($s) {
                       return str_replace("<br />", "", $s[0]);
               }
       }      
       $text = preg_replace_callback('/<pre>(.*?)<\/pre>/ms', "removeBr", $text);
       $text = preg_replace('/<p><pre>(.*?)<\/pre><\/p>/ms', "<pre>\\1</pre>", $text);
 
       $text = preg_replace_callback('/<ul>(.*?)<\/ul>/ms', "removeBr", $text);
       $text = preg_replace('/<p><ul>(.*?)<\/ul><\/p>/ms', "<ul>\\1</ul>", $text);
 
       return $text;
}
?>
 
 
[/code]


En línea



«Si quieres la paz prepárate para la guerra» Flavius Vegetius

[Taller]Instalación/Configuración y Teoría de Servicios en Red
Darkkz

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Re: Duda: Crear una pila contando palabras encontradas en un string
« Respuesta #2 en: 10 Septiembre 2011, 23:35 »

Mira si esto te sirve, es el BBCode Parser que viene con el editor Markitup.

Código
<?php
       // BBCode to find...
       $in = array(     '/\[b\](.*?)\[\/b\]/ms',      
                                        '/\[i\](.*?)\[\/i\]/ms',
                                        '/\[u\](.*?)\[\/u\]/ms',
                                        '/\[img\](.*?)\[\/img\]/ms',
                                        '/\[email\](.*?)\[\/email\]/ms',
                                        '/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms',
                                        '/\[size\="?(.*?)"?\](.*?)\[\/size\]/ms',
                                        '/\[color\="?(.*?)"?\](.*?)\[\/color\]/ms',
                                        '/\[quote](.*?)\[\/quote\]/ms',
                                        '/\[list\=(.*?)\](.*?)\[\/list\]/ms',
                                        '/\[list\](.*?)\[\/list\]/ms',
                                        '/\[\*\]\s?(.*?)\n/ms'
       );
       // And replace them by...
       $out = array(    '<strong>\1</strong>',
                                        '<em>\1</em>',
                                        '<u>\1</u>',
                                        '<img src="\1" alt="\1" />',
                                        '<a href="mailto:\1">\1</a>',
                                        '<a href="\1">\2</a>',
                                        '<span style="font-size:\1%">\2</span>',
                                        '<span style="color:\1">\2</span>',
                                        '<blockquote>\1</blockquote>',
                                        '<ol start="\1">\2</ol>',
                                        '<ul>\1</ul>',
                                        '<li>\1</li>'
       );
       $text = preg_replace($in, $out, $text);
?>
 
 

Con este sistema no tengo que hacer el follón del contador. Es más simple de lo que pensaba :-*

Resuelto, ¡Muchas gracias!
En línea
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Duda acerca de la pila en VB
Programación Visual Basic
Krnl64 4 457 Último mensaje 21 Julio 2006, 13:38
por Krnl64
[DUDA] mover a la pila
ASM
blackray 4 1,466 Último mensaje 10 Septiembre 2009, 03:07
por blackray
Crear funcion que cree palabras « 1 2 »
Programación C/C++
aguml 19 2,825 Último mensaje 26 Octubre 2009, 12:53
por aguml
nueva duda pila en C
Programación C/C++
pakleisito 13 1,970 Último mensaje 28 Mayo 2011, 16:50
por D4RIO
¿Extraer palabras de un string?
.NET
Lucas Cano 3 395 Último mensaje 17 Marzo 2012, 14:32
por Lucas Cano
Powered by SMF 1.1.16 | SMF © 2006-2008, Simple Machines