search
HomeBackend DevelopmentPHP TutorialExtract keywords from requested page

It can extract some keywords from a given URL retrieval web page

For example, from the homepage of Code Zhuji, keywords similar to the picture below can be extracted Extract keywords from requested page Extract keywords from requested page
  1. if(!empty($_REQUEST["url"])){
  2. include 'class.keywords.php';
  3. $keywords = new keywordsugest();
  4. $keywords- >_lang = 'es';
  5. $keywords->_encoding = 'iso-8859-1';
  6. $keywords->_catego = 'telecom';
  7. $keywords->_keyCount = 100; // is like the porcent %
  8. $keywords->file($_REQUEST['url']);
  9. #$keywords->readMetaKeyWords();
  10. #$keywords->readHtmlKeyWords();
  11. $keywords-> ;readAll();
  12. echo 'Keywords found :';
  13. $i = 1;
  14. foreach($keywords->get() as $word) echo $i++ .". $word
    ";
  15. }
  16. //url For example: http://www.codepearl.com
  17. echo "
    ";
  18. ?>
Copy code
  1. class keywordsugest{
  2. var $_html = FALSE;
  3. var $_keyCount = 5;
  4. var $_keyWords = array();
  5. var $_encoding = 'UTF-8';
  6. var $_lang = 'es';
  7. var $_catego = 'telecom';
  8. var $_url = '';
  9. /**
  10. * # read meta keywords
  11. *
  12. */
  13. public function readMetaKeyWords() {
  14. if (! $this->_html) return;
  15. preg_match('/"]*)"?[s]*[/]?[s]*>/is', $this->_html, $match);
  16. //$tags = get_meta_tags($this->_url);
  17. //echo $tags['keywords'];
  18. if (count($match)) {
  19. $this->_keyWords = array_unique(explode(',', preg_replace('/s/i', ' ', mb_strtolower($match[1], $this->_encoding))));
  20. }
  21. }
  22. /**
  23. * strip tags
  24. *
  25. * @param mixed $string
  26. */
  27. private function rip_tags($string) {
  28. // ----- remove HTML TAGs -----
  29. $string = preg_replace ('/]*>/', ' ', $string);
  30. /* // ----- remove control characters -----
  31. $string = str_replace("r", '', $string); // --- replace with empty space
  32. $string = str_replace("n", ' ', $string); // --- replace with space
  33. $string = str_replace("t", ' ', $string); // --- replace with space
  34. */
  35. // ----- remove multiple spaces -----
  36. $string = trim(preg_replace('/ {2,}/', ' ', $string));
  37. return $string;
  38. }
  39. /**
  40. * # read keywords from page body or string
  41. *
  42. */
  43. public function readHtmlKeyWords() {
  44. if (! $this->_html) return;
  45. if(!empty($this->_keyWords)){
  46. $implo = implode(' ',$this->_keyWords);
  47. $this->_html = $this->_html." ".$implo;
  48. $this->_keyWords = array();
  49. }
  50. $this->_html = str_replace(' ',' ', $this->_html);
  51. # remove unneeded parts
  52. $toRemove = array('head', 'script', 'style', 'object', 'embed', 'noembed', 'applet', 'noframes', 'noscript');
  53. foreach ($toRemove as $remove) $this->_html = preg_replace("/.*?/is", ' ', $this->_html);
  54. # remove comments
  55. $this->_html = preg_replace("//is", ' ', $this->_html);
  56. # delete html tags
  57. $this->_html = mb_strtolower($this->rip_tags($this->_html), $this->_encoding);
  58. $this->_html = htmlspecialchars_decode($this->_html);
  59. # decode encoded hmtl entities
  60. $this->_html = html_entity_decode ($this->_html, ENT_COMPAT, $this->_encoding);
  61. # break into words
  62. $words = preg_split("/[s]+|[t]+|[.]+|[,]+|[:]+|[;]+|[!]+|[?]+|[|]+/s", $this->_html, -1, PREG_SPLIT_NO_EMPTY);
  63. if (count($words)) {
  64. $frequency = array_count_values($words);
  65. unset($frequency['']);
  66. if (count($frequency)) {
  67. # delete stop words and interpunctions
  68. include('stopwords_'.$this->_lang.'.php');
  69. include('glodic_'.$this->_catego.'_'.$this->_lang.'.php');
  70. $punct = '~!@#$%^&*()_+|}{[];:'",<.>/?`-=\';
  71. foreach (array_keys($frequency) as $word) {
  72. if ( (in_array($word, $stopWords)) or (strspn($word, $punct) == strlen($word)) ){ unset($frequency[$word]); }
  73. }
  74. $max = max($frequency);
  75. $count = count($frequency);
  76. $tot = round(($max * 100) / $count);
  77. $tot2 = round(($this->_keyCount * 100) / $count);
  78. if($tot > $count){$tot = $tot / 2;}
  79. if($tot2 > $count){$tot = $tot / 2;}
  80. $showmax = round(($tot + $tot2) / 2);
  81. foreach (array_keys($frequency) as $word) {
  82. if ( in_array($word, $glodic) ){$frequency[$word] = $frequency[$word] + $showmax; }
  83. }
  84. # sort by frequency
  85. arsort($frequency, SORT_NUMERIC);
  86. # add them to keyword array
  87. $i = 0;
  88. foreach ($frequency as $word=>$count) {
  89. if ( (! in_array($word, $this->_keyWords)) &&
  90. (! is_numeric($word)) &&
  91. (! empty($word)) ) {
  92. $this->_keyWords[] = (string)$word;
  93. $i++;
  94. if ($i == $showmax) break;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * change the encoding from default utf-8
  102. *
  103. * @param mixed $enc
  104. */
  105. private function encoding($enc = FALSE) {
  106. if ($enc) $this->_encoding = $enc;
  107. }
  108. /**
  109. * # reads from file or url
  110. *
  111. * @param mixed $fileUrl
  112. */
  113. public function file($fileUrl = FALSE) {
  114. if ($fileUrl){ $this->_html = @file_get_contents($fileUrl);
  115. $this->_url = $fileUrl;
  116. }
  117. }
  118. /**
  119. * # define html as string
  120. *
  121. * @param mixed $page
  122. */
  123. public function html($page = FALSE) {
  124. if ($page) $this->_html = $page;
  125. }
  126. /**
  127. * # reads both meta keywords and from body
  128. *
  129. */
  130. public function readAll() {
  131. if ($this->_html !== FALSE) {
  132. $this->readMetaKeyWords();
  133. $this->readHtmlKeyWords();
  134. }
  135. $this->_keyWords = array_unique($this->_keyWords);
  136. }
  137. /**
  138. * # returns keywords as array
  139. *
  140. */
  141. public function get() {
  142. return $this->_keyWords;
  143. }
  144. }
  145. ?>
复制代码


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.