Advertisement
hackloper775

parser php

Jun 5th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. #!/usr/bin/php
  2.  
  3. <?php
  4. /*Reto : http://www.phperos.net/foro/index.php?board=20.20
  5.  * Problema 1 : Si detecta un comentario (#) no imprime nada
  6.  * Problema 2 : Si encuentra una variable ($var) cambia el valor de la impresion por nil
  7. */
  8. /*Algoritmo:
  9.  * Solucion 1 : Buscar el caracter (#) a lo largo del texto completo
  10.  * Sustituir su valor por nada
  11.  * Solucion 2 : Dividir variables en una array,ver el primer valor de array y compararlo con las reglas:
  12.  * " " // Mostrar espacios 2.1
  13.  * $ // Mostrar 2.2  $ -> $
  14.  * $$$ //Mostrar 2.3  $$$ -> $$$
  15.   * $var // Sustituir por nill 2.4 $variable -> nil
  16.  * $this-> // Notacion de fecha sustituir solo variable 2.5  $this->variables -> nil->variable
  17.  * No usar Regex
  18.  */
  19.  
  20. function parser($entrada) {
  21.     $cat = "";
  22.     $salida = "";
  23.     $vars = explode(" ", $entrada);
  24.     foreach ($vars as $v ) // Solucion 2
  25.     {
  26.         if ($v) // 2.1
  27.             {                                    
  28.                 if ($v[0] == '$') // 2.2
  29.             {
  30.                 if (strlen($v) > 1)
  31.                 {
  32.                     if ($v[1] == '$') // 2.3
  33.                     {
  34.                         $v = $v;
  35.                     }
  36.                     elseif (strstr($v,'-')) // 2.5
  37.                     {            
  38.                             $x = explode('-',$v);
  39.                             $x[0] = "nil";
  40.                             $v = $x[0] .  "-" . $x[1];
  41.                      }
  42.                     else // 2.4
  43.                     {
  44.                         $v = "nil";
  45.                     }
  46.                 }
  47.                 elseif (strlen($v) == 1)
  48.                 {
  49.                     $v = $v; // 2.2
  50.                 }
  51.             }
  52.         }
  53.         else
  54.         {
  55.             $salida .= $v;
  56.         }
  57.         $salida .= $v .= " ";
  58.     }
  59.     for ($i=0; $i < strlen($salida); $i++) {
  60.         if ($salida[$i] == '#' ) { // Solucion 1
  61.             $cat .= "";
  62.             break;
  63.         }
  64.         $cat .= $salida[$i ];
  65.     }
  66.  
  67.     $salida = $cat;
  68.  
  69.     return $salida ;
  70. }
  71.  
  72. echo parser('# hola soy un comentario $variable $$hola') . "\n";
  73. echo parser('    $soy_variable $$$ $y_yo_123') . "\n";
  74. echo parser('   $$$ $ $$$ $ $asd $343_34-holamundohola') . "\n";
  75.  
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement