Jump to content

: : How to format loads of words


anevins

Recommended Posts

Hi there,

 

I have a HUGE list of inappropriate words in plain text, but the thing is, I want to use these bad words in an array.

 

But I can't just copy and paste the bad words into the array, as each bad word needs to have two apostraphes aside one another.

 

E.g the array should be:

$bad_words = array('badword1','badword2','badword3');

 

but my text document just has:

badword1

badword2

badword3

 

How do I format the bad words to have apostraphes aside each other?

I don't want to manually go through each word.

 

Thanks

 

Link to comment
Share on other sites

If you have them in a text file, with each word on a separate line, you could just do this to create your array:

 

<?php

$file = "/path/to/file/bad_words.txt";
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));

$bad_words = explode("\n", $contents);

?>

Link to comment
Share on other sites

: : NEW Problem

 

I want to replace bad words which someone has inputted into my HTML textarea, with something like [CENSORED].

Here's what I've got so far:

note: $review is the posted textarea name.

$words_text = 'badwords.txt';
$bad_words = file($words_text);

$good_words = str_ireplace($bad_words,'[CENSORED]',$review);

echo $good_words;

 

The thing is, if I type bad words in the textarea, I still get bad words when I echo $good_words.

 

I don't know why I'm getting this, any ideas?

Link to comment
Share on other sites

try to trim array elements before use it

<?php
$words_text = 'badwords.txt';
$bad_words = file($words_text);
foreach($bad_words as $k => $v) $bad_words[$k] = trim($v);					
$good_words = str_ireplace($bad_words,'[CENSORED]',$review);

echo $good_words;

?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.