1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

search utility I want to create new array consisting of lines, containing search word

Discussion in 'PHP' started by t7584, Mar 20, 2006.

  1. #1
    I'm trying to create a search utility.
    $lines20 - an array consisting of gb.txt lines
    I want to create new array consisting of lines, containing search word - $var
    This program doesn't work. What's wrong with it?

    $var = @$_GET['q'] ;
    $lines20 = file ('gb.txt');
    $i=0;
    foreach ($lines20 as $rec20) {$pos = strpos($rec20, $var);

    if ($pos === true) {
    SEMrush

    $a20[$i]=$rec20;
    $i=$i+1;

    }
    }
    if ($i =0) {
    echo "0 results";

    }
     
    t7584, Mar 20, 2006 IP
    SEMrush
  2. Slapyo

    Slapyo Well-Known Member

    Messages:
    266
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You might want to try $pos !== false instead of $pos === true. === means you want the values to match as well as the type. strpos() returns the numeric position of where it finds the needle. An integer is not a boolean. Let me know if that works.

    $var = @$_GET['q'] ;
    $lines20 = file('gb.txt');
    $i=0;
    
    foreach($lines20 as $rec20) {
    	$pos = strpos($rec20, $var);
    	if ($pos !== false) {
    		$a20[$i] = $rec20;
    		$i = $i + 1;
    	}
    }
    
    if ($i = 0) {
    	echo "0 results";
    }
    Code (markup):
     
    Slapyo, Mar 20, 2006 IP
  3. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You might need to trim() each line before analyzing it to clear the newline character at the end.


    
    $line = trim($line);
    
    PHP:
    You might find these useful for finding text in strings. I didn't write these but found them a while back and they've been very useful.

    
    
         //////////  Case insensitive search
    
         function stri_contains($haystack, $needle)
         {
              $haystack = strtolower($haystack);
    
              $needle  = strtolower($needle);
    
              $needlePos = @strpos($haystack, $needle);
    
              return ($needlePos === false ? false : ($needlePos+1));
         }
    
    
    
    
    
         //////////  Case sensitive search
    
         function str_contains($haystack, $needle)
         {
              $needlePos = @strpos($haystack, $needle);
    
              return ($needlePos === false ? false : ($needlePos+1));
         }
    
    
    
    PHP:
     
    rvarcher, Mar 20, 2006 IP