Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- /*Reto : http://www.phperos.net/foro/index.php?board=20.20
- * Problema 1 : Si detecta un comentario (#) no imprime nada
- * Problema 2 : Si encuentra una variable ($var) cambia el valor de la impresion por nil
- */
- /*Algoritmo:
- * Solucion 1 : Buscar el caracter (#) a lo largo del texto completo
- * Sustituir su valor por nada
- * Solucion 2 : Dividir variables en una array,ver el primer valor de array y compararlo con las reglas:
- * " " // Mostrar espacios 2.1
- * $ // Mostrar 2.2 $ -> $
- * $$$ //Mostrar 2.3 $$$ -> $$$
- * $var // Sustituir por nill 2.4 $variable -> nil
- * $this-> // Notacion de fecha sustituir solo variable 2.5 $this->variables -> nil->variable
- * No usar Regex
- */
- function parser($entrada) {
- $cat = "";
- $salida = "";
- $vars = explode(" ", $entrada);
- foreach ($vars as $v ) // Solucion 2
- {
- if ($v) // 2.1
- {
- if ($v[0] == '$') // 2.2
- {
- if (strlen($v) > 1)
- {
- if ($v[1] == '$') // 2.3
- {
- $v = $v;
- }
- elseif (strstr($v,'-')) // 2.5
- {
- $x = explode('-',$v);
- $x[0] = "nil";
- $v = $x[0] . "-" . $x[1];
- }
- else // 2.4
- {
- $v = "nil";
- }
- }
- elseif (strlen($v) == 1)
- {
- $v = $v; // 2.2
- }
- }
- }
- else
- {
- $salida .= $v;
- }
- $salida .= $v .= " ";
- }
- for ($i=0; $i < strlen($salida); $i++) {
- if ($salida[$i] == '#' ) { // Solucion 1
- $cat .= "";
- break;
- }
- $cat .= $salida[$i ];
- }
- $salida = $cat;
- return $salida ;
- }
- echo parser('# hola soy un comentario $variable $$hola') . "\n";
- echo parser(' $soy_variable $$$ $y_yo_123') . "\n";
- echo parser(' $$$ $ $$$ $ $asd $343_34-holamundohola') . "\n";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement