|
ulf-wendel.de   
|
|
 Home < PHP Schulung  < Fortgeschrittenes PHP  < Regular Expressions  < PCRE: preg_*    |       |  
Print Version    
---
|
|
 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

<  ^  >

 Neues

 XML/XSLT Menu
 OOH-Form Rewrite

 PEAR Cache:
  SHM Container

 Suchstring Parser
 Buchrezensionen
 PEAR Cache:
  OutputCompression

 PEAR Menu Browser
 PEAR Menu Tutorial 
 PEAR Cache


 Tipp

Download Version:
oben rechts,
Download *.tar.gz
|
| --- |
|
  Top   |   <  ^  >   |   phpOpenTracker Statistik   |   URL: http://www.ulf-wendel.de/schulung/regexp/preg.php   |   Stand: 13.03.2001   |   © Ulf Wendel   
|
| --- |

0.042 s Bearbeitungszeit, 0.002 s IT[X], 0.003 s Menu 3