0

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?

2
  • Consider using strtr($body, $swears) (documentation: php.net/manual/en/function.strtr.php) for bad word replacement too. (Requires some alteration to the $swears array: $swear => $replacement) Commented Oct 29, 2010 at 18:49
  • Plus it usually won't work how you think it will: The Clbuttic Mistake... (Hint: replace a common "curse" with its more politically correct synonym)... Commented Oct 29, 2010 at 19:20

6 Answers 6

3

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);
Sign up to request clarification or add additional context in comments.

5 Comments

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);
Agreed - I would put the :/- part outside of the loop - but the question was how he could have that replacement inside the loop.
Furthermore - he doesn't need the loop. Just using the array $swears inside the str_ireplace() as first parameter could remove the loop entirely.
This is terrible, there is no reason for the foreach loop at all. You can pass arrays to str_ireplace.
Just posted a solution below, please check it.
3

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

Comments

1

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

1 Comment

Consider the audience. preg_replace is considerably more complicated that having two foreach statements
1

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);

2 Comments

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?
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.
0

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.

Comments

0

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);

3 Comments

I would +1, but you don't need an array of empty strings for the replacement part. A single '' string will suffice...
um, so why the downmod (ircmaxell hadn't downmodded with that comment)? At least comment to explain the problem.
I didn't downvote. In fact, +1 to cancel the downvote (since the edit looks great)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.