preg_match Probleme

Alles, was PHP betrifft, kann hier besprochen werden.

preg_match Probleme

Postby juergen&lx » 08. March 2005 19:04

Hallo zusammen,

ich bin leicht am verzweifeln,
für eine Überprüfung einer eingegebenen Zeit im Format 23:59:59 soll ein Gültigkeitstest in z.B. nachstehender Form realisiert werden.
Als Eingabe zum Testen: 12345 wird auf 8stellig geändert : 01:23:45
Eine Eingabe von 987654 hat das gleiche Ergebnis 98:76:54
Code: Select all
--->8--snip
if (preg_match("|^([0-1][0-9]|[2][0-3])':'[0-5][0-9]':'[0-5][0-9]$|",$timestring,$matches)) {
      return $matches[0];
---8<---snap

Auch ein suchen bei http://de2.php.net/manual/de/function.preg-match.php war nicht oder besser nur tlw. aufschlussreich.
Vielleicht weiss jemand mehr an dieser Stelle.
Im voraus vielen Dank

Jürgen
juergen&lx
 
Posts: 154
Joined: 18. February 2004 21:36
Location: in einem Dorf am Rande des Strombergs
Operating System: Linux

Postby juergen&lx » 08. March 2005 23:50

ich habe das Problem so gelöst
Code: Select all
Funktionsaufruf mit Fehlerauswertung:
     $playtime0 = $user->spaceStr($playtime,':',2 );
      if ($playtime0 == "invalid hours") {
        $message = "<font class=red>Nicht plausibel: Stundenwert ist zu hoch.</font>";
        $action1="";
        $act = "";
      }
      elseif ($playtime0 == "invalid min") {
        $message = "<font class=red>Nicht plausibel: Minutenwert ist zu hoch.</font>";
        $action1="";
        $act = "";
      }
      elseif ($playtime0 == "invalid sek") {
        $message = "<font class=red>Nicht plausibel: Sekundenwert ist zu hoch.</font>";
        $action1="";
        $act = "";
      }
      else { $playtime=$playtime0;
      }


Code: Select all
    function spaceStr($string,$space,$pos) {
      $cpos=$pos;
      if (strlen($string) ==6 ) {
      }
      elseif (strlen($string) ==5 ) {
        $string='0'.$string;
      }
      elseif (strlen($string) ==4 ) {
        $string='00'.$string;
      }
      elseif (strlen($string) <= 3 ) { return " time to low";
      }
       while ($cpos<strlen($string))
      {
        $string=substr($string,0,$cpos).$space.substr($string,$cpos);
        $cpos+=strlen($space)+$pos;
      };
      if (substr($string, 0, 2 ) >= 12 ) { return "invalid hours";
      }
      elseif (substr($string, 3, 2) > 59 ) { return "invalid min";
      }
      elseif (substr($string, 6, 2) > 59 ) { return "invalid sek";
      }
      else { return $string;
      }
    }


Hätt' man ja auch gleich drauf kommen können, oder :)
Jürgen
juergen&lx
 
Posts: 154
Joined: 18. February 2004 21:36
Location: in einem Dorf am Rande des Strombergs
Operating System: Linux


Return to PHP

Who is online

Users browsing this forum: No registered users and 20 guests