Results 1 to 6 of 6

Thread: How would i

  1. #1
    Join Date
    Aug 2007
    Posts
    185
    Tokens
    0

    Default How would i

    How would i check if a variable contains a word in an array?

    Thread moved by mat64 (Forum Super Moderator) from Designing & Development: Please post in the correct section
    Last edited by mat64; 21-01-2008 at 12:06 AM.

  2. #2
    Join Date
    Jun 2005
    Posts
    2,688
    Tokens
    0

    Latest Awards:

    Default

    $array = array('words','to','search','for');
    $string = 'words i would like to search';
    if(in_array($string,$array)) {
    //do something
    }else{
    //not in array
    }

  3. #3
    Join Date
    Aug 2007
    Posts
    185
    Tokens
    0

    Default

    That doesn't making it if it contains it

    For example if i put the word "hi" in the array, if the variable was just "hi" it would select it but if the variable was for example "lolhi" it wouldn't, i want it so if the word even just contains it.

  4. #4
    Join Date
    May 2005
    Location
    San Francisco, CA
    Posts
    7,160
    Tokens
    2,331

    Latest Awards:

    Default

    Nevermind.

    Edited by mat64 (Forum Super Moderator): Please don't post pointlessly
    Last edited by mat64; 21-01-2008 at 12:07 AM.

  5. #5
    Join Date
    Jun 2005
    Posts
    2,688
    Tokens
    0

    Latest Awards:

    Default

    PHP Code:
    $array = array('words','to','search','for');
    $string 'words i would like to search';

    $i=0;
    $rules '(';
    while(
    $i<count($array)) {
    if(
    $i!=count($array)) {
    $rules .= $array[$i].'|';
    }else{
    $rules .= $array[$i];
    }
    $i++;
    }
    $rules .= ')';
    if(
    preg_match("/".$rules."/",$string)) {
    //Would create rule /(words|to|search|for)/

    //do something

    }else{

    //do something else if not found


    try that

  6. #6
    Join Date
    May 2006
    Posts
    1,797
    Tokens
    0

    Latest Awards:

    Default

    Id do..

    PHP Code:
    <?php
    $array 
    = array("word1","word2","word3");
    $string "Variable string thing";
     
    foreach(
    $array as $v1)
    {
    if(
    eregi($string,$v1))
    $i++;
    }
     
    if(
    $i>0)
    echo(
    "This word is in the array!");
    else
    echo(
    "It isnt in the array!");
    ?>
    Coming and going...
    Highers are getting the better of me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •