PHP/String/preg match all

Материал из Web эксперт
Перейти к: навигация, поиск

A non-capturing optional subpattern

   <source lang="html4strict">

<?php $html = "<link rel="icon" href="http://www.example.ru/icon.gif"/> <link rel="prev" title="Previous" href="http://www.example.ru/prev.xml"/> <link rel="next" href="http://www.example.ru/next.xml"/>"; preg_match_all("/rel="(?:prev|next)"(?: title="[^"]+?")? href= "([^"]*?)"/", $html, $linkMatches); print "$bothMatches is: "; var_dump($linkMatches); ?>

 </source>
   
  


Capturing HTML headings

   <source lang="html4strict">

<?php $html = file_get_contents("example.html"); preg_match_all("@<h([1-6])>(.+?)</h\1>@is", $html, $matches); foreach ($matches[2] as $text) {

   print "Heading: $text \n";

} ?>

 </source>
   
  


Extracting text from HTML tags

   <source lang="html4strict">

<?php $html = file_get_contents("example.html"); preg_match_all("@<(strong|em)>(.+?)</\1>@is", $html, $matches); foreach ($matches[2] as $text) {

   print "Text: $text \n";

} ?>

 </source>
   
  


Finding Matches Globally with preg_match_all()

   <source lang="html4strict">

<? $text = "take, tart, till"; if ( preg_match( "/\bt\w+s\b/", $text, $array ) ) {

print "
\n";
  print_r( $array );
  print "
\n";

} ?>

 </source>
   
  


Finding the nth match

   <source lang="html4strict">

<?php $todo = "1. a 2. B 3. C"; preg_match_all("/\d\. ([^\d]+)/", $todo, $matches); print "The second item on the todo list is: "; print $matches[1][1]; print "The entire todo list is: "; foreach($matches[1] as $match) {

   print "$match\n";

} ?>

 </source>
   
  


Making a quantifier match as few characters as possible

   <source lang="html4strict">

<?php // find all emphasized sections preg_match_all("@.+?@", $html, $matches); ?>

 </source>
   
  


Matching with preg_match_all()

   <source lang="html4strict">

<? $html = <<<_HTML_

  • BeefFun
  • Pea
  • Noodles

_HTML_;

preg_match("@
  • (.*?)
  • @",$html,$matches); $match_count = preg_match_all("@
  • (.*?)
  • @",$html,$matches_all);

    print "preg_match_all() matched $match_count times.\n"; print "preg_match() array: "; var_dump($matches); print "preg_match_all() array: "; var_dump($matches_all); ?>

     </source>
       
      
    


    m modifier: match an anchored pattern on multiple lines of text.

       <source lang="html4strict">
    
    

    The anchor patterns ^ and $ match the beginning and end of an entire string by default.

    <? $text = "name: Joe\noccupation: coder\n\n"; if ( preg_match_all( "/^\w+:\s+(.*)$/m", $text, $array ) ) {

    print "
    \n";
      print_r( $array );
      print "
    \n";

    } ?>

     </source>
       
      
    


    preg_match_all

       <source lang="html4strict">
    
    

    <?php

      $userinfo = "Name: PHP 
    Title: Programming Language"; preg_match_all ("/(.*)<\/b>/U", $userinfo, $pat_array); print $pat_array[0][0]."
    ".$pat_array[0][1]."\n";

    ?>

     </source>
       
      
    


    preg_match_all demo

       <source lang="html4strict">
    
    

    <?

       $a = "Foo moo boo tool foo!";
       preg_match_all("/[A-Za-z]oo\b/i", $a, $matches);
       var_dump($myarray);
    

    ?>

     </source>
       
      
    


    preg_match_all() function matches all occurrences of pattern in string.

       <source lang="html4strict">
    
    

    Its syntax: int preg_match_all (string pattern, string string, array pattern_array [, int order]) There are two possible types of order: PREG_PATTERN_ORDER is the default. PREG_PATTERN_ORDER: $pattern_array[0] is an array of all complete pattern matches, $pattern_array[1] is an array of all strings matching the first parenthesized regexp, and so on. PREG_SET_ORDER: $pattern_array[0] contains elements matched by the first parenthesized regexp, $pattern_array[1] contains elements matched by the second parenthesized regexp, and so on. <? $userinfo = "Name: <b>R
    Title: PHP"; preg_match_all ("/(.*)<\/b>/U", $userinfo, $pat_array); print $pat_array[0][0]."
    ".$pat_array[0][1]."\n"; ?>

     </source>
       
      
    


    preg_match_all searches a string for all the occurrences of a regular expression

       <source lang="html4strict">
    
    

    <?php $s = "A beautiful day"; preg_match_all ("/beaut[^ ]+/", $s, $matches); var_dump ($matches) ?>

     </source>
       
      
    


    Reading Records with a Delimiter

       <source lang="html4strict">
    
    

    <?php

     $file = fopen("testfile.csv", "r") or die("Cannot open file!\n");
     while ($line = fgets($file, 1024)) {
       preg_match_all("/[^,\"]+|\"([^\"]|\"\")*\"/", $line, $fields);
       echo "First field is:  " . $fields[0][0] . "\n";
       echo "Second field is: " . $fields[0][1] . "\n";
     }
     fclose($file);
    

    ?>

     </source>
       
      
    


    Using preg functions

       <source lang="html4strict">
    
    

    <?php if (preg_match("{<title>.+</title>}", $html)) {

       // page has a title
    

    }

    if (preg_match_all("/
  • /", $html, $matches)) { print "Page has " . count($matches[0]) . " list items\n"; } // turn bold into italic $italics = preg_replace("/(<\/?)b(>)/", "$1i$2", $bold); ?> </source>

    Using preg_match_all() to Match a Pattern Globally

       <source lang="html4strict">
    
    

    <html> <head> <title>Using preg_match_all() to Match a Pattern Globally</title> </head> <body> <?php $text = "plants, pianos and parrots"; if ( preg_match_all( "/\bp\w+s\b/", $text, $array ) ) {

    print "
    \n";
      print_r( $array );
      print "
    \n";

    } ?> </body> </html>

    </source>