Find Word In Variable, Change Another Var To 1

Discussion in 'PHP' started by gahoo, Nov 1, 2007.

  1. #1
    How do I make it so if it finds the word News in a variable such as $text, it sets the $category variable to 1?

    Thanks!
     
    gahoo, Nov 1, 2007 IP
  2. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Try this:

    
    if ( strpos( $text, "News" ) !== false ) $category = 1;
    
    PHP:
    Brew
     
    Brewster, Nov 1, 2007 IP
  3. gahoo

    gahoo Peon

    Messages:
    837
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! Is that case sensitive?
     
    gahoo, Nov 1, 2007 IP
  4. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Yes it is. If you want it to be case insensitive use stripos()

    Brew
     
    Brewster, Nov 1, 2007 IP
  5. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #5
    According to the manual stripos() is php 5 only. If you are using php 4 use this code:

    $SearchWord = 'news';
    	
    if ( preg_match( "/$SearchWord/i", $text ) ) $category = 1;
    PHP:
    Brew
     
    Brewster, Nov 1, 2007 IP
  6. gahoo

    gahoo Peon

    Messages:
    837
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Works now! Thanks!
     
    gahoo, Nov 1, 2007 IP