Log in

View Full Version : Arrays Question clearification and filter...



Rockonmetal
11-20-2007, 10:09 PM
Ok, Arrays have been a bit difficult for me to understand how they work and what they can do for me...



$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:


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
$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

djr33
11-20-2007, 10:46 PM
Ok, here's how arrays work.

$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).

boogyman
11-21-2007, 02:20 PM
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


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

Rockonmetal
11-24-2007, 04:37 PM
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
$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?

thetestingsite
11-24-2007, 05:17 PM
Get rid of the quotes around the variables:



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.

Rockonmetal
11-24-2007, 05:23 PM
oh! so take the quotes out... ok i will thanks!
It works lol...
its really odd now but anyways...

<?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!

Rockonmetal
11-24-2007, 05:32 PM
<?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:

/*
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

thetestingsite
11-24-2007, 05:39 PM
Try this, change $_POST['comment'] to $_GET['comment'] for testing like so:

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



<?php
$badwords = array('some','bad','words','here');

//change the input string to all lowercase letters
$userstr = strtolower (http://php.net/strtolower)($_POST['comment']);

$output = str_replace($badwords, 'EXPLICT', $userstr);

/* Display the output */
echo ucfirst (http://www.php.net/ucfirst)($output);
?>


Hope this helps.

Rockonmetal
11-24-2007, 06:09 PM
Last question I swear

I want to check the length of the comment to make sure its long enough...
I currently have this 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

thetestingsite
11-24-2007, 06:15 PM
To do this, you would want to do something like the following:



if (count_chars (http://php.net/countchars)($lower) >= 3 && count_chars (http://php.net/countchars)($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 (http://php.net/countchars)($lower) . " letters";
}


Hope this helps.

djr33
11-25-2007, 07:50 AM
I am pretty sure there wouldnt be a dot unless you declare one.Oh, I was vague. I meant . in the sense of php "link" or whatever that operation is called, where $v1.$v2 is "[value1][value2]", ie, run together, no dot output.