Unexpected $end error in (last line of script ).. please help! :)

Discussion in 'PHP' started by d1srupt3r, Jun 12, 2008.

  1. #1
    Hello guys, I having a hard time what this error causes in my script. Please see the following codes:
    // create ending array
    $endings = array(
    "ves" => array("fe", "f"),
    "fs" => array ("f"),
    "os" => array("o"),
    "oes" => array("o"),
    "ies" => array("y"),
    "xes" => array("x", "xe"),
    "as" => array("a"),
    "ae" => array("a"),
    "rs" => array("r"),
    "es" => array(""),
    "s" => array(""),
    );
    
    foreach ($endings as $ploral_end => $singulars) {
         if( substr($neword, - strlen($ploral_end)) == $plural_end ) {
              foreach ($singulars as $replace_singular) {
                   $tryword = substr($neword, 0, strlen($neword) - strlen($ploral_end)) . $replace_singular; 
                   //check_for_word is your word lookup function
                   if ( check_for_word($tryword) ) {
                   $neword = $tryword;
                   break;
              }
         }
    Code (markup):
    I can't figure it out because it pointed that error is in the last line of the script.
    Thank you guys for helping me out. :D
     
    d1srupt3r, Jun 12, 2008 IP
  2. RNK Concepts

    RNK Concepts Peon

    Messages:
    74
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this code:

    
    // create ending array
    $endings = array(
    "ves" => array("fe", "f"),
    "fs" => array ("f"),
    "os" => array("o"),
    "oes" => array("o"),
    "ies" => array("y"),
    "xes" => array("x", "xe"),
    "as" => array("a"),
    "ae" => array("a"),
    "rs" => array("r"),
    "es" => array(""),
    "s" => array(""),
    );
    
    foreach ($endings as $ploral_end => $singulars) {
         if( substr($neword, - strlen($ploral_end)) == $plural_end ) {
              foreach ($singulars as $replace_singular) {
                   $tryword = substr($neword, 0, strlen($neword) - strlen($ploral_end)) . $replace_singular;
                   //check_for_word is your word lookup function
                   if ( check_for_word($tryword) ) {
                   $neword = $tryword;
                   break;
              }
         };
    
    Code (markup):
    Let me know if that helps!
     
    RNK Concepts, Jun 12, 2008 IP
  3. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It still produces the same error. :(
    Thanks for helping me out.
     
    d1srupt3r, Jun 12, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    // create ending array
    $endings = array(
    "ves" => array("fe", "f"),
    "fs" => array ("f"),
    "os" => array("o"),
    "oes" => array("o"),
    "ies" => array("y"),
    "xes" => array("x", "xe"),
    "as" => array("a"),
    "ae" => array("a"),
    "rs" => array("r"),
    "es" => array(""),
    "s" => array(""),     //<-------take off the last commar from the array
    );
    
    foreach ($endings as $ploral_end => $singulars) {
         if( substr($neword, - strlen($ploral_end)) == $plural_end ) {
              foreach ($singulars as $replace_singular) {
                   $tryword = substr($neword, 0, strlen($neword) - strlen($ploral_end)) . $replace_singular; 
                   //check_for_word is your word lookup function
                   if ( check_for_word($tryword) ) {
                   $neword = $tryword;
                   break;
              }//if
         }//for
      }//if
    }//for
    PHP:
     
    php-lover, Jun 12, 2008 IP
  5. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay the error was gone, thanks php-lover! But why is it my script doesn't work? It doesn't replace queries that end in "es" "ves" and such? Is there something wrong with the construction of the code?
     
    d1srupt3r, Jun 12, 2008 IP
  6. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #6
    $plural_end not $ploral_end :)

    You should owe me....jk
     
    php-lover, Jun 12, 2008 IP
  7. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    LOL. You got me there.. Thanks for the help guys but my script still doesn't work. I don't know if I wrote a correct function please take a look again. :)

    
    $slashedword = stripslashes($_REQUEST['w']);
    $neword = preg_replace('%\ $%', '', $slashedword);
    
    // create ending array
    $endings = array(
    "ves" => array("fe", "f"),
    "fs" => array ("f"),
    "os" => array("o"),
    "oes" => array("o"),
    "ies" => array("y"),
    "xes" => array("x", "xe"),
    "as" => array("a"),
    "ae" => array("a"),
    "rs" => array("r"),
    "es" => array(""),
    "s" => array("")
    );
    
    foreach ($endings as $plural_end => $singulars) {
         if( substr($neword, - strlen($plural_end)) == $plural_end ) {
              foreach ($singulars as $replace_singular) {
                   $tryword = substr($neword, 0, strlen($neword) - strlen($plural_end)) . $replace_singular; 
    
    				function check_for_word() {
    				$matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC');
    				$foundword = mysql_fetch_array( $matching );
    
    				}//function
    
                   //check_for_word is your word lookup function
                   if ( check_for_word($tryword) ) {
                   $word = $foundword;
                   break;
              }//if
         }//for
      }//if
    }//for
    
    //Find the word again that matched previously
    $result = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $word).'" ORDER BY ss_type ASC');
    .....and so on...
     
    Code (markup):
    This error shows up:
     
    d1srupt3r, Jun 12, 2008 IP
  8. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #8
    $slashedword = stripslashes($_REQUEST['w']);
    $neword = preg_replace('%\ $%', '', $slashedword);
    
    // create ending array
    $endings = array(
    "ves" => array("fe", "f"),
    "fs" => array ("f"),
    "os" => array("o"),
    "oes" => array("o"),
    "ies" => array("y"),
    "xes" => array("x", "xe"),
    "as" => array("a"),
    "ae" => array("a"),
    "rs" => array("r"),
    "es" => array(""),
    "s" => array("")
    );
    
    foreach ($endings as $plural_end => $singulars) {
         if( substr($neword, - strlen($plural_end)) == $plural_end ) {
              foreach ($singulars as $replace_singular) {
                   $tryword = substr($neword, 0, strlen($neword) - strlen($plural_end)) . $replace_singular;
                   
                   //check_for_word is your word lookup function
                   if ( check_for_word($tryword) ) {
                   $word = $tryword;
                   break;
              }//if
         }//for
      }//if
    }//for
    
    
    
    function check_for_word($tryword) {
    
    	$matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC');
    	if(is_resource($matching)){
    	
    	  $result = (mysql_num_rows($matching) > 0)true:false;
    	  return $result;
    	  
    	}//if
    }//function
    PHP:
     
    php-lover, Jun 12, 2008 IP
  9. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    There is an error, it says
    The line is in:
    $result = (mysql_num_rows($matching) > 0)true:false;
    Code (markup):
    What does that mean?
     
    d1srupt3r, Jun 12, 2008 IP
  10. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #10
    $result = (mysql_num_rows($matching) > 0)true:false;

    $result = (if number of result row is greater than zero) then return true else return false;


    Can you post the error line number.
     
    php-lover, Jun 12, 2008 IP
  11. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I see. Here's the actual error:

    The line 81 is the bolded texts:
    // create ending array
    $endings = array(
    "ves" => array("fe", "f"),
    "fs" => array ("f"),
    "os" => array("o"),
    "oes" => array("o"),
    "ies" => array("y"),
    "xes" => array("x", "xe"),
    "as" => array("a"),
    "ae" => array("a"),
    "rs" => array("r"),
    "es" => array(""),
    "s" => array("")
    );
    
    foreach ($endings as $plural_end => $singulars) {
         if( substr($neword, - strlen($plural_end)) == $plural_end ) {
              foreach ($singulars as $replace_singular) {
                   $tryword = substr($neword, 0, strlen($neword) - strlen($plural_end)) . $replace_singular;
                   
                   //check_for_word is your word lookup function
                   if ( check_for_word($tryword) ) {
                   $word = $tryword;
                   break;
              }//if
         }//for
      }//if
    }//for
    
    function check_for_word($tryword) {
    
        $matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC');
        if(is_resource($matching)){
        
    [B]      $result = (mysql_num_rows($matching) > 0)true:false;[/B]
          return $result;
          
        }//if
    }//function 
    Code (markup):
    The actual php script is this.
    I am sorry, I am still learning php. I hope you understand. :)
     
    d1srupt3r, Jun 12, 2008 IP
  12. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #12
    
    //Not a problem. Replace the function with this code 
    
    function check_for_word($tryword) {
    
    	$matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC');
    	if(is_resource($matching)){
    	
    	  if(mysql_num_rows($matching) > 0)
    	     return true;
    	  else
    	     return false;	  
    	  
    	}//if
    
    }//function
    PHP:
     
    php-lover, Jun 12, 2008 IP
  13. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    The previous error was gone, but a new error came up:
    The bolded text is the line:
    // create ending array
    $endings = array(
    "ves" => array("fe", "f"),
    "fs" => array ("f"),
    "os" => array("o"),
    "oes" => array("o"),
    "ies" => array("y"),
    "xes" => array("x", "xe"),
    "as" => array("a"),
    "ae" => array("a"),
    "rs" => array("r"),
    "es" => array(""),
    "s" => array("")
    );
    
    foreach ($endings as $plural_end => $singulars) {
         if( substr($neword, - strlen($plural_end)) == $plural_end ) {
              foreach ($singulars as $replace_singular) {
                   $tryword = substr($neword, 0, strlen($neword) - strlen($plural_end)) . $replace_singular;
                   
                   //check_for_word is your word lookup function
                   [B]if ( check_for_word($tryword) ) {[/B]
                   $word = $tryword;
                   break;
              }//if
         }//for
      }//if
    }//for
    
    function check_for_word($tryword) {
    
        $matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC');
        if(is_resource($matching)){
        
          if(mysql_num_rows($matching) > 0)
             return true;
          else
             return false;    
          
        }//if
    
    }//function 
    Code (markup):
    PS:
    I have solved the problem. I put function check_for_word before the if ( check_for_word($tryword) and it seems it is working now but it still has minor problems. When I searched for words that ends in s such as "seasons" it return as to "season" but the rest of the endings in the array seems doesn't replace the query?
     
    d1srupt3r, Jun 12, 2008 IP
  14. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    After a lot of hours searching for the answer, I found the solution. :) I will share it to you because this one is new to me. The solution is to take the function always outside a loop. Pretty clever right? Why? Functions declared in a function are global, so calling the container function twice will attempt to create the same function in the global scope. -from E-E.com

    So lesson for the day: Take out the functions outside a loop. ;)
     
    d1srupt3r, Jun 13, 2008 IP