Jump to content

[SOLVED] printing from a text file to a page if...


lyax

Recommended Posts

<?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.  ???

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.

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)) {

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.