Edited by Rahul Dev, 13 January 2011 - 10:13 PM.
Summarize text article in PHP
Started by Rahul Dev, Dec 09 2010 02:43 AM
4 replies to this topic
#1
Posted 09 December 2010 - 02:43 AM
111
|
|
|
#2
Posted 09 December 2010 - 02:59 PM
You mean something like this? I modified the script to better an example:
Examples:
$text = "This is a sentence. This is also a line. This is another line. This is an example." ;
<?php
$title = "This is an example of a sentence in a paragraph";
$title_array = explode(" ", $title);
$title_words = array();
foreach($title_array as $word) {
if(strlen($word) > 5) {
$title_words[] = $word;
}
}
$text = "This is a sentence. This is also a line. This is another line. This an example." ;
$sentence = strtok($text, ".?!");
$summary = false;
while ($sentence !== false){
if($summary === false) {
foreach($title_words as $word) {
if(strstr($sentence, $word)) {
$summary = $sentence;
}
}
}
echo $sentence.".";
$sentence = strtok(".?!");
}
print '<br/>Summary: ' . $summary . '.';
?> This will find the first occurrence of the word in $title_words and store the appropriate sentence into $summary, it will only store the first found sentence within $summary.Examples:
$text = "This is a sentence. This is also a line. This is another line. This is an example." ;
Summary: This is a sentence.$text = "These are some words. This is also a line. This is another line. This is an example." ;
Summary: This is an example.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 10 December 2010 - 06:53 AM
111
Edited by Rahul Dev, 13 January 2011 - 10:15 PM.
#4
Posted 10 December 2010 - 05:34 PM
Rahul Dev said:
Thanx a lot for the help! That's what i needed. But actually i wanted the sentences to add up in $summary so that i can display it as a paragraph.
This is a sentence. This is also a line. This is another line. This an example. Summary: This is a sentence. This an example.
<?php
$title = "This is an example of a sentence in a paragraph";
$title_array = explode(" ", $title);
$title_words = array();
foreach($title_array as $word) {
if(strlen($word) > 5) {
$title_words[] = $word;
}
}
$text = "This is a sentence. This is also a line. This is another line. This an example." ;
$sentence = strtok($text, ".?!");
$summary = '';
while ($sentence !== false){
foreach($title_words as $word) {
if(strstr($sentence, $word)) {
$summary .= $sentence . '. ';
}
}
echo $sentence.".";
$sentence = strtok(".?!");
}
print '<br/>Summary: ' . $summary;
?>
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 10 December 2010 - 08:30 PM
111
Edited by Rahul Dev, 13 January 2011 - 10:17 PM.


Sign In
Create Account

Back to top









