[SOLVED] Filtering the output of an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

[SOLVED] Filtering the output of an array

Post by Daisy Cutter »

I've got an array output code that looks like this:

Code: Select all

foreach ($Array as $key => $val) {
$band = array("bad","words","here");
if(strpos($val, $band) !== FALSE) {
$val = str_replace($band,"***",$val);
echo $val; }
else {
echo $val;
} 
echo "<br />"; }
I want that to be able to take any words in the $band array and if they are found, replace them with ***.

If I change $band in the strpos() and str_repace() functions with a word in quotes, like "hello" it works, but using the array variable does not.

Could anyone tell me how I could achieve this goal?
Thanks!
Last edited by Daisy Cutter on Fri Dec 16, 2005 10:32 pm, edited 1 time in total.
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

nevermind... what a fool i was being :oops:

Code: Select all

foreach ($Array as $key => $val) { 
$band = array("bad","words","here"); 
$val = str_replace($band,"***",$val); 
echo $val;
echo "<br />"; }
That works just fine. No idea why I decided to try and check if the string was found first, since if it wasn't there it wouldn't be replaced anyway. I guess it's just been a long day.
Post Reply