Horje
How to extract words from Sentence in PHP

Extract Words like keywords from Sentence by PHP


PHP Function for extracting Words

Just use it in functional. Create a functions.php file and Insert following codes
functions.php
Example: PHP
<?php
// Extract Tag
function extractCommonWords($string){
      $stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');
   
      $string = preg_replace('/\s\s+/i', '', $string); // replace whitespace
      $string = trim($string); // trim the string
      $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
      $string = strtolower($string); // make it lowercase
   
      preg_match_all('/\b.*?\b/i', $string, $matchWords);
      $matchWords = $matchWords[0];
      
      foreach ( $matchWords as $key=>$item ) {
          if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 5 ) {
              unset($matchWords[$key]);
          }
      }   
      $wordCountArr = array();
      if ( is_array($matchWords) ) {
          foreach ( $matchWords as $key => $val ) {
              $val = strtolower($val);
              if ( isset($wordCountArr[$val]) ) {
                  $wordCountArr[$val]++;
              } else {
                  $wordCountArr[$val] = 1;
              }
          }
      }
      arsort($wordCountArr);
      $wordCountArr = array_slice($wordCountArr, 0, 10);
      return $wordCountArr;
}
?>

Here you can result out of texts from sentence

Just Copy and Paste following code to index.php Make a index.php file

index.php
Example: PHP
<?php
include("functions.php");
$text = 'Cricket is the Most Popular Game in the World';
$array = extractCommonWords($text);
echo 'Tag:';
foreach($array as $k => $k) {
echo '<span class="badger badger-darkener"><a href="'.$url.'/browse/'.strtolower($catss).''.strtolower($k).'" style="color:blue;">#'.$k.'</a></span> ';
} 
?>

Output should be:

Here you can result out of texts from sentence





Related Articles
How to check if a string contain multiple specific words PHP String Tutorial
How to extract words from Sentence in PHP PHP String Tutorial
How to check if string is_numeric() Function in PHP PHP String Tutorial
How to get only text from HTML Source Code by PHP PHP String Tutorial
How to get first 3 words from a sentence by PHP PHP String Tutorial
How to work if empty value does not else PHP String Tutorial
How to remove first Five Line from a TXT File in PHP PHP String Tutorial
How to get Longest Word from a Sentence in PHP PHP String Tutorial
How to remove first character from string php PHP String Tutorial
How to count if Number is below than 10 sum PHP String Tutorial
How to get back page link PHP String Tutorial
How to get first 3 longest words from a sentence PHP PHP String Tutorial
How to count number of words from sentence in PHP PHP String Tutorial
How to remove all number from a string php PHP String Tutorial
How to create first word Upper/Big from a sentence PHP String Tutorial

Single Articles
PHP Function for extracting WordsPHP String Tutorial
Here you can result out of texts from sentencePHP String Tutorial

Read Full:
PHP String Tutorial
Category:
Web Tutorial
Sub Category:
PHP String Tutorial
Uploaded by:
Admin
Views:
96
Tested on:
PHP 7