lyax Posted April 26, 2007 Share Posted April 26, 2007 <?php // start $want = "gene"; $demo = array( "This is a test line with the word gene in it", "Genetic Enginnering is interesting but no match", "This line has Gene with a capital G", "The General Bus Company", "If you are looking for a multi-gene solution, this may be it", "Gene Kelly and Doris Day may not be what you're looking for", "gentle example", "gene", "another gentle example", "This is a line almost ending in gene.", "This is a line ending in gene", "Then, gene again") "This last line has a last word with g-e-n-e at the end; engene"); foreach ($demo as $trythis) { if (eregi("(^|[- \('])$want([- ,.\)?]|$)",$trythis)) { print $trythis."<br>\n"; } } // or print "<hr>"; foreach ($demo as $trythis) { if (preg_match("/\\b$want\\b/i",$trythis)) { print $trythis."<br>\n"; } } // End. ?> How to change this script as below: 1-print sentences if their first word is gene. (6. and 8. - NOT 2.) 2-print sentences if their second word is gene (12. one - NOT 4.) 3-print ... third... attention to commas or dots. need help. thanks. ??? Link to comment https://forums.phpfreaks.com/topic/48797-solved-printing-from-a-text-file-to-a-page-if/ Share on other sites More sharing options...
Psycho Posted April 26, 2007 Share Posted April 26, 2007 Just use explode to convert each sentence to an array then you can check 1st, 2nd, 3rd, etc. values <?php foreach ($demo as $sentence) { $words = explode(' ', $sentence); if ($words[0]==$want) { echo "First Word match: $sentence"; } if (strtoupper($words[1])==strtoupper($want)) { echo "Second Word match: $sentence"; } //Etc } ?> EDIT: I just realized there is problem with the above. It works with your examples, but if you had the sentence "Gene, did you take out the garbage" then there would not be a match because "GENE" != "GENE,". So you need to decide how you want to handle this. Do you want to exclude anything other than letters from the comparrison? Will the $want value ever have anything other than letters? You need to clearly define what $want can be and how you want to handle the logic. Link to comment https://forums.phpfreaks.com/topic/48797-solved-printing-from-a-text-file-to-a-page-if/#findComment-239201 Share on other sites More sharing options...
Psycho Posted April 26, 2007 Share Posted April 26, 2007 OK, you can change the comparrison to this to only compare the letters in the "word" to the $want value. Of course, that means the $want value cannot contain anything other than letters: if (strtoupper(eregi_replace('[^a-zA-Z]', '', $words[0]))==strtoupper($want)) { Link to comment https://forums.phpfreaks.com/topic/48797-solved-printing-from-a-text-file-to-a-page-if/#findComment-239220 Share on other sites More sharing options...
per1os Posted April 26, 2007 Share Posted April 26, 2007 To fix the case issue above this should be good: <?php foreach ($demo as $sentence) { $words = explode(' ', strtolower($sentence)); $want = strtolower($want); if ($words[0]==$want) { echo "First Word match: $sentence"; } if (isset($words[1]) && $words[1]==$want) { echo "Second Word match: $sentence"; } //Etc } ?> There is probably a better way to do it, but that works. Link to comment https://forums.phpfreaks.com/topic/48797-solved-printing-from-a-text-file-to-a-page-if/#findComment-239224 Share on other sites More sharing options...
Psycho Posted April 26, 2007 Share Posted April 26, 2007 To fix the case issue above this should be good: <?php foreach ($demo as $sentence) { $words = explode(' ', strtolower($sentence)); $want = strtolower($want); if ($words[0]==$want) { echo "First Word match: $sentence"; } if (isset($words[1]) && $words[1]==$want) { echo "Second Word match: $sentence"; } //Etc } ?> There is probably a better way to do it, but that works. Didn't I already handle the case issue? Except I used strtoupper(). The problem is having a character such as a comma or period next to the search term. It can be handled, but the criteria for handling it needs to be decided. Link to comment https://forums.phpfreaks.com/topic/48797-solved-printing-from-a-text-file-to-a-page-if/#findComment-239431 Share on other sites More sharing options...
lyax Posted April 27, 2007 Author Share Posted April 27, 2007 many thanks for your help Link to comment https://forums.phpfreaks.com/topic/48797-solved-printing-from-a-text-file-to-a-page-if/#findComment-239795 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.