Rahul Dev Posted December 17, 2010 Share Posted December 17, 2010 Hello Guys can anyone help me with this? <?php $common_words = array(" the ", " is "); $title = " The dog is chasing the boy "; $title = strtolower($title); $title_array = explode(" ", $title); $title_words = array(); foreach($title_array as $word) { $title_words[] = str_replace($common_words, " ", $title); } $text = " The dog is chasing the boy. This is an irrelevant sentence. The boy runs away from the dog. This is another irrelevant sentence. "; $sentence = strtok($text, ".?!"); $summary = ''; while ($sentence !== false){ foreach($title_words as $word) { if(strstr($sentence, $word)) { $summary .= $sentence . '. '; } } $sentence = strtok(".?!"); } echo $summary; ?> when i execute this code i get a blank page. I think the problem is on this line: $title_words[] = str_replace($common_words, " ", $title); The expected output is: The dog is chasing the boy. The boy runs away from the dog. Quote Link to comment https://forums.phpfreaks.com/topic/221987-problem-with-string/ Share on other sites More sharing options...
MatthewJ Posted December 17, 2010 Share Posted December 17, 2010 Maybe I'm just lost, but there seem to be any number of issues here... First: $common_words = array(" the ", " is "); There you are creating an array of two "common words" to replace... except you have spaces before and after them... but, when you explode the string to work on, you explode it on the space which effectively removes them. " the " and "the" are not the same string, so your replace is not going to find any matches. Not to mention you have a space at the beginning and end of the test string... so the first element of the exploded array is blank and useless. Then, your loop is going to run each word in the test string, and attempt to replace the matched words (again, they won't match.. so it won't replace anything and you will end up with an array of the same $title strings over and over with no change). Next, I'm not positive... but I think strtok() requires a token, it looks like you're trying to pass a set of three tokes, I'm not sure it works that way... so strtok is probably looking for .?! and is not finding it as that combination neverappears in the $text var. Last, you check the values (which need to be fixed first anyway I do believe) against $text... but you never do strtolower() on the $test var so "The" does not match "the" and so on as again, they are different strings. But again, it's early and I may be confusing myself Quote Link to comment https://forums.phpfreaks.com/topic/221987-problem-with-string/#findComment-1148706 Share on other sites More sharing options...
Rahul Dev Posted December 17, 2010 Author Share Posted December 17, 2010 ok so i've made some changes and here's the whole codes: <?php $common_words = array( " a ", " able ", " about ", "across ", " after ", " all ", " almost ", " also ", " am ", " among ", " an ", " and ", " any ", " are ", " as " , " at ", " be ", " because ", " been ", " but ", " by ", " can ", " cannot ", " could ", " dear ", " did ", " do ", " does ", " either ", " else ", " ever ", " every ", " for ", " from ", " get ", " got ", " had ", " has ", " have ", " he ", " her ", " hers ", " him ", " his ", " how ", " however ", " i ", " if ", " in ", "into ", " is ", " it ", " its ", " just ", " least ", " let ", " like ", " likely ", " may ", " me ", " might ", " most ", " must ", " my ", " neither ", " no ", " nor ", " not ", " of ", " off ", " often ", " on ", " only ", " or ", " other ", " our ", " own ", " rather ", " said ", " say ", " says ", " she ", " should ", " since ", " so ", " some ", " than ", " that ", " the ", " their ", " them ", " then ", " there ", " these ", " they ", " this ", " tis ", " to ", " too ", " us ", " wants ", " was ", " we ", " were ", " what ", " when ", " where ", " which ", " while ", " who ", " whom ", " why ", " will ", " with ", " would ", " yet ", " you ", " your "); $title = "Japan knife attacker injures at least 13 people"; $title = strtolower($title); $title_words = array(); $new_title = str_replace($common_words, " ", $title); $title_array = explode(" ", $new_title); foreach($title_array as $word) { $title_words[] = $word; } $text = "Japanese police have arrested a man after a knife attack outside a train station that left at least 13 people injured. The man boarded two buses and attacked passengers - who were predominantly school children - in the city of Toride, some 40km (25 miles) north-east of Tokyo, reports say. Four people were stabbed, and others were injured as they fled, police say. A 27-year-old man has been arrested on suspicion of attempted murder. One of the victims would need to remain in hospital for several weeks, while the others were not seriously injured, said a spokesman for the local fire department. Correspondents say the attack has brought back memories of an incident in central Tokyo in 2008, in which a man armed with a knife killed seven people."; $sentence = strtok($text, ".?!"); $summary = ''; while ($sentence !== false){ foreach($title_words as $word) { if(strstr($sentence, $word)) { $summary .= $sentence . '. '; } } $sentence = strtok(".?!"); } echo $summary; ?> What i'm trying to do here is remove the common words from the $title then for each word in $new_title that appears in a sentence of the $text i should add that sentence to the $summary. But if more than one word from $new_title appears in a sentence that sentence is repeated the number of times the words in the $new_title appears in the text. how can i make that sentence appear only once? Quote Link to comment https://forums.phpfreaks.com/topic/221987-problem-with-string/#findComment-1148750 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.