Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Arrays Question clearification and filter...

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default Arrays Question clearification and filter...

    Ok, Arrays have been a bit difficult for me to understand how they work and what they can do for me...

    PHP Code:
    $sentence = array("Part","of","an","array"); 
    Thats an example of an array...
    I am trying to create a filter for my site's comment board...
    *Does anyone know a link to like all the bad words in the universe lol... cuz i don't use a variety of language...*
    But, instead of doing this:
    PHP Code:
    if($comment==badwordthatidon'tfeellikegettingbannedfor){
      echo "****";

    Then looping that... which would not be fun...

    I saw this in W3 schools... but it has got arrays in it *which confuse me...*
    if someone could explain how this calls the individual parts...
    PHP Code:
    <?php
    $arr 
    = array("Hello" => "Hi""world" => "earth");
    echo 
    strtr("Hello world",$arr);
    ?>
    I think I've got a thought on this...
    So if
    $arr = array("Hello" => "Hi", "world" => "earth");
    Basically the => says turn this to that... then that to that...
    Am I correct? Thanks...
    Thanks
    Last edited by Rockonmetal; 11-24-2007 at 04:39 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ok, here's how arrays work.
    PHP Code:
    $a = array($key1=>$value1,$key2=>$value2);
    echo 
    $a[$key1]; //'$value1'
    echo $a[$key2]; //'$value2'
    $b = array($value1,$value2);
    echo 
    $a[0]; //starts at 0, '$value1'
    echo $a[1]; //'$value2'

    foreach($b as $val) {
    echo 
    $val;
    }
    //'$value1.$value2'

    foreach($b as $key=>$val) {
    echo 
    $key.$val;
    }
    //'$key1.$value1.$key2.$value2'

    $a['test'] = 'abc';

    echo 
    $a['test']; //'abc'
    //note that the older values of $a are still stored 
    The easiest way to replace each element is with a foreach loop. However, you could actually just use str_replace(), as it allows array arguments (see the documentation for details).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    foreach($b as $val) {
    echo $val;
    }
    //'$value1.$value2'

    foreach($b as $key=>$val) {
    echo $key.$val;
    }
    //'$key1.$value1.$key2.$value2'
    I am pretty sure there wouldnt be a dot unless you declare one. it would look something more like
    Code:
    ex1) $val1$val2$val3$val4
    ex2) $key1.$value1$key2.$value2$key3.$value3$key4.$value4
    but I am also sure that you would probably be using some other type of html styling before you actually render the page, which of course would have its own default markup style

  4. #4
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Question

    Ok... thats a little to complicated... lol... uh... but was my concept at the top correct??? If i am going to do this, which i tried but it didn't work for me...
    PHP Code:
    <?php
    $words 
    = array("Peter","Quagmire","Joe");
    $Description "Peter ate Quagmire and Joe then ate Peter for eating Quagmire...";
    echo 
    str_replace("$words","EXPLICT","$Description");
    ?>
    How can i get it so that it works?
    It doesn't seem to filter the words...
    When i do the echo for $words, it just returns as Array...
    Which doesn't make any sense...
    Any reason?

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Get rid of the quotes around the variables:

    Code:
    echo str_replace("$words","EXPLICT","$Description");
    When you have quotes around the variables it means you are trying to search for the word '$words' in the string '$Description'; in this case returning false.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    oh! so take the quotes out... ok i will thanks!
    It works lol...
    its really odd now but anyways...
    Code:
    <?php
    $words = array("Peter","Quagmire","Joe");
    $Description = "Peter ate Quagmire and Joe then ate Peter for eating Quagmire...";
    echo str_replace($words,"EXPLICT",$Description);
    echo $words;
    
    ?>
    K thanks!

  7. #7
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Code:
    <?php
    $words = array("Peter","Quagmire","Joe");
    $Description = "Peter ate Quagmire and Joe then ate Peter for eating Quagmire...";
    echo str_replace($words,"EXPLICT",$Description);
    echo $words;
    
    ?>
    K now a quick question...
    I know that the str_replace is case sensitive and some idiot is gonna do this
    baDwoRd and it will won't filter... is there anyway I can get it so that only stuff like this:
    Code:
    /*
    SEnTaNcE WorD Dude BaD woRD *User inputted stuff*
    Turns into this
    Sentance word dude bad word... *Draft*
    Sentance word dude EXPLETIVE... *Final*
    */
    Either that or is there a function/command/tag/something like str_replace that isn't case sensitive?

    Thanks

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try this, change $_POST['comment'] to $_GET['comment'] for testing like so:

    script.php?comment=Your StRiNG Goes HerE With SOmE BAd WorDS

    Code:
    <?php
    $badwords = array('some','bad','words','here');
    
    //change the input string to all lowercase letters
    $userstr = strtolower($_POST['comment']);
    
    $output = str_replace($badwords, 'EXPLICT', $userstr);
    
    /* Display the output */
    echo ucfirst($output);
    ?>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Last question I swear

    I want to check the length of the comment to make sure its long enough...
    I currently have this code
    Code:
    if($length<=100 and $length<=3){
    echo "<textarea>";
    echo str_replace($words," * ",$lower);
    echo "</textarea>";
    }else{
    echo "You have entered in " . $length . " letters";
    }
    I don't think this is a valid line
    if($length<=100 and $length<=3)
    I do this because if $length meets both conditions then it will perform the if statement...
    I learned this type of stuff in math class *the concept of inequalities not the code...*
    That something like this
    x<4 and x>1 means that all the numbers between 1 and 4 will work for both inequalities...
    Any help?
    Thanks

  10. #10
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    To do this, you would want to do something like the following:

    Code:
    if (count_chars($lower) >= 3 && count_chars($lower) <= 100) { //if the characters are more than or equal to 3 but less than or equal to 100
    
    echo "<textarea>";
    echo str_replace($words," * ",$lower);
    echo "</textarea>";
    }else{
    echo "You have entered in " . count_chars($lower) . " letters";
    }
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

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