elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  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 2,316 veces)
Darkkz

Desconectado Desconectado

Mensajes: 2


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

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
  1. function str_postea($cadena1,$cadena2,$texto) {
  2. $contador=0;
  3. while(preg_match('/'.$cadena1.'/',$texto))
  4. $contador++;
  5. while(preg_match('/'.$cadena2.'/',$texto))
  6. $contador--;
  7. while($contador>0) {
  8. $texto = $texto.$cadena2;
  9. $contador--;
  10. }
  11. while($contador<0) {
  12. $texto = str_replace($cadena2,"",$texto);
  13. $contador++;
  14. }
  15.  
  16. return $texto;
  17. }
Código
  1. $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
Colaborador
***
Desconectado Desconectado

Mensajes: 1.911



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

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

Código
  1.  
  2. <?php
  3. // ----------------------------------------------------------------------------
  4. // markItUp! BBCode Parser
  5. // v 1.0.6
  6. // Dual licensed under the MIT and GPL licenses.
  7. // ----------------------------------------------------------------------------
  8. // Copyright (C) 2009 Jay Salvat
  9. // http://www.jaysalvat.com/
  10. // http://markitup.jaysalvat.com/
  11. // ----------------------------------------------------------------------------
  12. // Permission is hereby granted, free of charge, to any person obtaining a copy
  13. // of this software and associated documentation files (the "Software"), to deal
  14. // in the Software without restriction, including without limitation the rights
  15. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. // copies of the Software, and to permit persons to whom the Software is
  17. // furnished to do so, subject to the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be included in
  20. // all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. // THE SOFTWARE.
  29. // ----------------------------------------------------------------------------
  30. // Thanks to Arialdo Martini, Mustafa Dindar for feedbacks.
  31. // ----------------------------------------------------------------------------
  32.  
  33. define ("EMOTICONS_DIR", Site::get_url('habari') . "/user/plugins/markup/markitup/parsers/bbcode/images/");
  34.  
  35. function BBCode2Html($text) {
  36.        $text = trim($text);
  37.  
  38.        // BBCode [code]
  39.        if (!function_exists('escape')) {
  40.                function escape($s) {
  41.                        global $text;
  42.                        $text = strip_tags($text);
  43.                        $code = $s[1];
  44.                        $code = htmlspecialchars($code);
  45.                        $code = str_replace("[", "&#38;#91;", $code);
  46.                        $code = str_replace("]", "&#38;#93;", $code);
  47.                        return '<pre><code>'.$code.'</code></pre>';
  48.                }      
  49.        }
  50.        $text = preg_replace_callback('/\[code\](.*?)\[\/code\]/ms', "escape", $text);
  51.  
  52.        // Smileys to find...
  53.        $in = array(     ':)',
  54.                                         ':D',
  55.                                         ':o',
  56.                                         ':p',
  57.                                         ':(',
  58.                                         ';)'
  59.        );
  60.        // And replace them by...
  61.        $out = array(    '<img alt=":)" src="'.EMOTICONS_DIR.'emoticon-happy.png" />',
  62.                                         '<img alt=":D" src="'.EMOTICONS_DIR.'emoticon-smile.png" />',
  63.                                         '<img alt=":o" src="'.EMOTICONS_DIR.'emoticon-surprised.png" />',
  64.                                         '<img alt=":p" src="'.EMOTICONS_DIR.'emoticon-tongue.png" />',
  65.                                         '<img alt=":(" src="'.EMOTICONS_DIR.'emoticon-unhappy.png" />',
  66.                                         '<img alt=";)" src="'.EMOTICONS_DIR.'emoticon-wink.png" />'
  67.        );
  68.        $text = str_replace($in, $out, $text);
  69.  
  70.        // BBCode to find...
  71.        $in = array(     '/\[b\](.*?)\[\/b\]/ms',      
  72.                                         '/\[i\](.*?)\[\/i\]/ms',
  73.                                         '/\[u\](.*?)\[\/u\]/ms',
  74.                                         '/\[img\](.*?)\[\/img\]/ms',
  75.                                         '/\[email\](.*?)\[\/email\]/ms',
  76.                                         '/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms',
  77.                                         '/\[size\="?(.*?)"?\](.*?)\[\/size\]/ms',
  78.                                         '/\[color\="?(.*?)"?\](.*?)\[\/color\]/ms',
  79.                                         '/\[quote](.*?)\[\/quote\]/ms',
  80.                                         '/\[list\=(.*?)\](.*?)\[\/list\]/ms',
  81.                                         '/\[list\](.*?)\[\/list\]/ms',
  82.                                         '/\[\*\]\s?(.*?)\n/ms'
  83.        );
  84.        // And replace them by...
  85.        $out = array(    '<strong>\1</strong>',
  86.                                         '<em>\1</em>',
  87.                                         '<u>\1</u>',
  88.                                         '<img src="\1" alt="\1" />',
  89.                                         '<a href="mailto:\1">\1</a>',
  90.                                         '<a href="\1">\2</a>',
  91.                                         '<span style="font-size:\1%">\2</span>',
  92.                                         '<span style="color:\1">\2</span>',
  93.                                         '<blockquote>\1</blockquote>',
  94.                                         '<ol start="\1">\2</ol>',
  95.                                         '<ul>\1</ul>',
  96.                                         '<li>\1</li>'
  97.        );
  98.        $text = preg_replace($in, $out, $text);
  99.  
  100.        // paragraphs
  101.        $text = str_replace("\r", "", $text);
  102.        $text = "<p>".preg_replace("/(\n){2,}/", "</p><p>", $text)."</p>";
  103.        $text = nl2br($text);
  104.  
  105.        // clean some tags to remain strict
  106.        // not very elegant, but it works. No time to do better ;)
  107.        if (!function_exists('removeBr')) {
  108.                function removeBr($s) {
  109.                        return str_replace("<br />", "", $s[0]);
  110.                }
  111.        }      
  112.        $text = preg_replace_callback('/<pre>(.*?)<\/pre>/ms', "removeBr", $text);
  113.        $text = preg_replace('/<p><pre>(.*?)<\/pre><\/p>/ms', "<pre>\\1</pre>", $text);
  114.  
  115.        $text = preg_replace_callback('/<ul>(.*?)<\/ul>/ms', "removeBr", $text);
  116.        $text = preg_replace('/<p><ul>(.*?)<\/ul><\/p>/ms', "<ul>\\1</ul>", $text);
  117.  
  118.        return $text;
  119. }
  120. ?>
  121.  
  122.  
[/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 pm »

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

Código
  1. <?php
  2.        // BBCode to find...
  3.        $in = array(     '/\[b\](.*?)\[\/b\]/ms',      
  4.                                         '/\[i\](.*?)\[\/i\]/ms',
  5.                                         '/\[u\](.*?)\[\/u\]/ms',
  6.                                         '/\[img\](.*?)\[\/img\]/ms',
  7.                                         '/\[email\](.*?)\[\/email\]/ms',
  8.                                         '/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms',
  9.                                         '/\[size\="?(.*?)"?\](.*?)\[\/size\]/ms',
  10.                                         '/\[color\="?(.*?)"?\](.*?)\[\/color\]/ms',
  11.                                         '/\[quote](.*?)\[\/quote\]/ms',
  12.                                         '/\[list\=(.*?)\](.*?)\[\/list\]/ms',
  13.                                         '/\[list\](.*?)\[\/list\]/ms',
  14.                                         '/\[\*\]\s?(.*?)\n/ms'
  15.        );
  16.        // And replace them by...
  17.        $out = array(    '<strong>\1</strong>',
  18.                                         '<em>\1</em>',
  19.                                         '<u>\1</u>',
  20.                                         '<img src="\1" alt="\1" />',
  21.                                         '<a href="mailto:\1">\1</a>',
  22.                                         '<a href="\1">\2</a>',
  23.                                         '<span style="font-size:\1%">\2</span>',
  24.                                         '<span style="color:\1">\2</span>',
  25.                                         '<blockquote>\1</blockquote>',
  26.                                         '<ol start="\1">\2</ol>',
  27.                                         '<ul>\1</ul>',
  28.                                         '<li>\1</li>'
  29.        );
  30.        $text = preg_replace($in, $out, $text);
  31. ?>
  32.  
  33.  

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 1,862 Último mensaje 21 Julio 2006, 13:38 pm
por Krnl64
[DUDA] mover a la pila
ASM
blackray 4 3,652 Último mensaje 10 Septiembre 2009, 03:07 am
por blackray
¿Extraer palabras de un string?
.NET (C#, VB.NET, ASP)
dont'Exist 3 4,359 Último mensaje 17 Marzo 2012, 14:32 pm
por dont'Exist
Ayuda con varias palabras en un string
Programación C/C++
kur79 6 2,294 Último mensaje 11 Septiembre 2015, 11:16 am
por ivancea96
[C++] (Consulta) ¿std::string guarda la cadena en el heap o en la pila?
Programación C/C++
class_OpenGL 7 2,757 Último mensaje 10 Agosto 2016, 17:10 pm
por class_OpenGL
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines