Home  PHP Projekte  PHP Schulung  Informationsquellen  Geschichte  Core PHP  Fortgeschrittenes PHP  Datenbankzugriff  Regular Expressions  POSIX: ereg_*  PCRE: preg_*  Stringfunktionen  Templates  Cache-Technologien  OOH Forms  Technik der Site  Büchertipps  Fotografie  Airbrush  Kontakt  Stuff 
|
Perl kompatible Regular Expressions
- Suchen: preg_match(), preg_match_all(), preg_grep()
- Ersetzen: preg_relplace()
- Zerlegen: preg_split()
- Hilfreich: preg_quote()
| PCRE: preg_* |
Top |
|
<?php // E-Mail auf Gültigkeit testen $email = "php-general@lists.php.net"; // case insensitive über Modifier i if ( preg_match('/[a-z]([-a-z0-9_.])*@([-a-z0-9_]*\.)+[a-z]{2, }/i', $email ) ) print "Die E-Mail ist gültig.\n";
print "\n";
// HTML Dokument zerlegen $html = '<body bgcolor="#ff0000"><h1>PCRE</h1></body>'; preg_match_all("@<body[^>]*>(.*)</body>@siU", $html, $regs); var_dump($regs); print "\n";
// Soziale Kontakte pflegen print preg_replace( "@([\w]+\s[\w]+\s[\w]+)(\s[\w]+)(\s[\w]+)@", ",\n,\n,\n!", "Happy birthday to you John." );
print "\n\n"; // Texte zerlegen: \s = " " + \t + \n + \r $text = "Otto Mustermann,\nBeispielfirma."; $words = preg_split("/\s/", $text); print_r($words); print "\n";
// preg_replace() akzeptiert auch Arrays - schnell, Templates! $template = "<html><head><title></title></head><body></body></html>"; $regs = array( "@@", "@@" ); $repl = array( "Meine Homepage", "01.04.2030" ); $template = preg_replace($regs, $repl, $template); print $template; ?>
|
|
Ausgabe
|
Top |
array(2) {
[0]=>
array(1) {
[0]=>
string(44) "PCRE"
}
[1]=>
array(1) {
[0]=>
string(13) "PCRE"
}
}
Happy birthday to you,
Happy birthday to you,
Happy birthday to John,
Happy birthday to you!.
Array
(
[0] => Otto
[1] => Mustermann,
[2] => Beispielfirma.
)
Meine Homepage01.04.2030 |
|
|
< ^ >
|