Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created October 21, 2018 04:24
Show Gist options
  • Save phpfiddle/418c3239a4d2e85be8cea7fd0782af66 to your computer and use it in GitHub Desktop.
Save phpfiddle/418c3239a4d2e85be8cea7fd0782af66 to your computer and use it in GitHub Desktop.
[ Posted by Martin ] Word analysing in texts
<?php
$analyze = '';
$punctuations = '.:-,;–_?!';
if (isset($_POST['analyze']) and trim($_POST['analyze']) != '') {
$analyze = $_POST['analyze'];
$analyzed = preg_replace( "/\r|\n/", " ", $analyze );
$len = strlen($analyze);
$pl = strlen($punctuations);
for ($i=0; $i < $pl; $i++) {
$analyzed = str_replace($punctuations[$i], ' ', $analyzed);
}
$words = array_filter(explode(' ', $analyzed));
$result = [
'characters' => $len,
'count' => count($words),
'differents' => count(array_unique($words)),
];
echo '<pre>' . print_r($result, true) . '</pre>';
}
?>
<!-- Your forms -->
<form action="" method="POST" class="myForm">
<textarea placeholder="Dein Text" name="analyze" style="width: 100%;" rows="10"><?php echo $analyze; ?></textarea>
<input type="submit" id="submit" value="analysieren" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment