tags:

views:

91

answers:

6

Here is my current code:

foreach ($swears as $bad_word)
$body = str_ireplace($bad_word, "", $body);

It's filtering bad words, but I'd like to also filter a ":" to a "-" in the body. How can I have multiple foreach statements in my script?

+3  A: 

Put them right after each other?

Eg:

foreach($swears as $bad_word)
    $body = str_ireplace($bad_word, '', $body);

$replace_chars = array(
    ':' => '-',
    '?' => '!');
foreach($replace_chars as $char => $rep)
    $body = str_replace($char, $rep, $body);

If you only have one additional character you want to replace, just use str_replace() again, by itself, outside of the foreach(), instead of using the $replace_chars array and the second foreach().

mway
-1, please explain?
mway
+1  A: 

You should just try using http://php.net/manual/en/function.preg-replace.php

Aaron Hathaway
Consider the audience. `preg_replace` is considerably more complicated that having two `foreach` statements
bemace
You have a point. /concede
Aaron Hathaway
+2  A: 

Use curly brackets?

foreach( $swears as $bad_word )
{
  $body = str_ireplace($bad_word, "", $body);
  $body = str_ireplace(":", "-", $body);
}

or arrays in str_ireplace:

foreach( $swears as $bad_word )
  $body = str_ireplace(array(":", $bad_word), array("-", ""), $body);
Repox
Why would you loop over the `:` replace? It will do something the first time, and then make no further modifications. It's best to put that outside the loop. Or even better, don't use a loop: `$body = str_ireplace($swears, '', $body); $body = str_replace(':', '-', $body);`
ircmaxell
Agreed - I would put the :/- part outside of the loop - but the question was how he could have that replacement inside the loop.
Repox
Furthermore - he doesn't need the loop. Just using the array $swears inside the str_ireplace() as first parameter could remove the loop entirely.
Repox
This is terrible, there is no reason for the foreach loop _at all_. You can pass arrays to str_ireplace.
jusunlee
Just posted a solution below, please check it.
jusunlee
A: 

I don't see why you'd need another foreach for that.

foreach ($swears as $bad_word)
    $body = str_ireplace($bad_word, "", $body);

$body = str_replace(":", "-", $body);

But if you did there's nothing stopping you from having another one.

bemace
A: 

You can use arrays in stri_replace

$body = str_ireplace($bad_words, '', $body);
$body = str_replace(':', '-', $body);

Another way to do it with a single replace, which works well if you have more filter arrays (you could use array_merge to add more replacements)

$filters = $bad_words;
$replacements = array_fill(0, count($bad_words), '');

$filters[] = ':';
$replacements[] = '-';

$body = str_ireplace($filters, $replacements, $body);
enobrev
I would +1, but you don't need an array of empty strings for the replacement part. A single `''` string will suffice...
ircmaxell
ah, didn't know that. thanks irc! Updated accordingly.
enobrev
um, so why the downmod (ircmaxell hadn't downmodded with that comment)? At least comment to explain the problem.
enobrev
+1  A: 

All the responses are terrible. You don't need a foreach loop. Here's how it should be done:

 $filter = array(
    ':'      => '-',
    'badword'    => '',
    'anotherbad' => ''
);
$body = str_ireplace(array_keys($filter), $filter, $body);
jusunlee
Should I now downvote *you* since you could have done `$body = str_ireplace(array(':','badword','anotherbad'),array('-'), $body);`? Or do you just want to settle down a little?
bemace
Sorry, but that's a less than ideal implementation. Try editing a filter list of 100 with that method! It would be too cumbersome. The method posted makes it easy to add and modify the filter list anytime in the future.
jusunlee