Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple PHP Q?

Options
  • 27-07-2001 10:03pm
    #1
    Closed Accounts Posts: 2


    I'm trying to check a file to see if it contains a word. There is just one word on each line in the external file. The "word" I am searching for is passed as a variable to the page called "word". My guess is that the "error" in on line 6. Is it possible to do this comparison in PHP?

    1 if($word) {
    2 $words = file("dictionary.html");
    3 $sizeof = count($words);
    4
    5 for($i=0; $i < $sizeof; $i++) {
    6 if($word == $file_contents[$i]) {
    7 $flag=1;
    8 } else {
    9 print ("$domain does not equal $file_contents[$p]<br>\n");
    10 }
    11 }
    12 if($flag) {
    13 print ("This word has been found.");
    14 } else {
    15 print ("Sorry, the word is not in this page.");
    16 }
    17
    18 }


    Looking forward to your helpful replies!


    reteP

    I before E except after C. This is a weird society we live in.


Comments

  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    For a start:
    2 $words = file("dictionary.html");
    

    Will return an array, so you would want to run through:
    6 if($word == $words[$i]) {
    

    Also, I don’t know if an exact match is the best thing to be looking for as each element in the array will also include a carriage return (+line feed if you’re running the Win32 CGI version), not to mention HTML tags (it is a HTML file you’re searching after all). You may want to use another comparison method, such as stristr(), strstr() or substr().

    Also, a few other things of note:

    $flag should really be defined outside of the if/then conditional as it will remain undefined if no match is found. It probably should also be defined as a boolean rather than an integer. Also you refer to $p on line 9 - I assume this should be $i. And on line 9, I assume $domain should be $word.

    Hope this helps.

    "Just because I'm evil doesn't mean I'm not nice." - Charlie Fulton


  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    Here's a question - where did the variable $file_contents spring from? Also. you don't need parenthesis when you're using print() (or echo()).
    if($word) {
    	$words = file("dictionary.html");
    	// Initializing $flag is good coding procedure
    	$flag = 0;
    	// Unless you need a count later, you can count() inside the for construct.
    	for ($i=0; $i&lt;count($words); $i++) {
    		// You need to trim() off whitespace or you won't get a match.
    		if ($word == trim($words[$i])) {
    			$flag = 1;
    		} else {
    			// Why are you printing here? Debugging?
    			print "$domain does not equal $file_contents[$p]&lt;br&gt;\n";
    		}
    	}
    	if($flag) {
    		print "This word has been found.";
    	} else {
    		print "Sorry, the word is not in this page.";
    	}
    }
    

    HTH,
    adam


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    <font face="Verdana, Arial" size="2">Originally posted by dahamsta:
    Here's a question - where did the variable $file_contents spring from?</font>
    Looking at the use of $domain later in the script, I'd guess $words was originally $file_contents when he cut 'n pasted the script from a snarfed script.

    "Just because I'm evil doesn't mean I'm not nice." - Charlie Fulton

    [This message has been edited by The Corinthian (edited 30-07-2001).]


Advertisement