Tags zählen

Beispiel ausführen
Ein Beispiel um Tags in einem String zu zählen. Hier kommt der e-Modifiers in preg_replace() zum Einsatz. Eine denkbare Verwendung ist die schnelle schematische Analyse von HTML- oder XML-Quelltext.
<?php

$theContent 
'<h1>Ein einfaches Skript um Tags zu zählen.</h1><p>Absatz 1</p><p><strong>Absatz 2</strong></p>';

if (isset(
$_POST['CONTENT'])){

    
$theContent stripslashes($_POST['CONTENT']);
    
$counter = array();
    
$success preg_replace ('#<([a-z1-6]+)[^>]*>#ie'
                 
'isset($counter["\\1"]) ? $counter["\\1"]++ :$counter["\\1"]=1'
                 
$theContent
            
);

    if (
$success){
    
        print (
'<h2>Nach erstem Vorkommen geordnet (<em>document order</em>)</h2>');
        foreach (
$counter as $k => $v){
            
printf ('<h3>%s <em>%s</em></h3>'$k$v);
        }
        
ksort($counter);

        print (
'<h2>Alphabetisch geordnet</h2>');
        foreach (
$counter as $k => $v){
            
printf ('<h3>%s <em>%s</em></h3>'$k$v);
        }

        
arsort($counter);

        print (
'<h2>Nach Häufigkeit geordnet</h2>');
        foreach (
$counter as $k => $v){
            
printf ('<h3>%s <em>%s</em></h3>'$k$v);
        
        }

        print (
'<h2>Auswertung</h2>');
        
printf ('Es wurden %d unterschiedliche, und insgesamt %d Tags gezählt.'count($counter), array_sum($counter));
        
        
        

    }
    
$theContent htmlentities($theContent);
}


echo <<< EOT
<form action="{$_SERVER['PHP_SELF']}" method="post">
 <table>
  <tr>
   <td><textarea name="CONTENT">
{$theContent}</textarea></td>
  </tr>
  <tr>
   <td><input type="submit"></td>
  </tr>
 </table>
</form>
EOT;
?>

Benutzte Funktionen


Hinweise zum DatenschutzImpressum © 2005-2008 S. Eickhoff