O'Reilly logo

Reguläre Ausdrücke kurz & gut, 2nd Edition by Tony Stubblebine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Beispiele

//Suche von Spider-Man, Spiderman, SPIDER-MAN usw.
$dailybugle = "Spider-Man Menaces City!";

$regex = "/spider[- ]?man/i";

if (preg_match($regex, $dailybugle)) {
      //hier passiert etwas
}
Listing 1.13 Einfaches Matching
///Datumsangaben in Formaten wie MM/DD/YYYY,
///MM-DD-YY ... erkennen
$date = "12/30/1969";
$p    = "!^(\\d\\d)[-/](\\d\\d)[-/](\\d\\d(?:\\d\\d)?)$!";

if (preg_match($p,$date,$matches) {
      $monat = $matches[1];
      $tag   = $matches[2];
      $jahr  = $matches[3];
}
Listing 1.14 Matching- und Capture-Gruppe
//<br> XHTML-konform in <br /> umwandeln
$text = "Hallo Welt! <br>";

$pattern  = "{<br>}i";

echo preg_replace($pattern, "<br />", $text);
Listing 1.15 Einfache Substitution
//urlify - URLs in HTML-Links umwandeln $text = "Besuchen Sie die Website ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required