Jump to content

Help with PHP Script!

- - - - -

  • Please log in to reply
2 replies to this topic

#1
elliottveares

elliottveares

    Newbie

  • Members
  • Pip
  • 5 posts
Hi there, I need some help with a PHP script, in my htdocs folder within apache i have a php file titled filter.php

The script is supposed to search for a given word with in a text file and remove the given word, i.e. I want to use it to remove swear words from the text file which contains comments. Each comment in the text file is separated by a line.

(Here is my script, it does not seem to be working when I run it, other PHP scripts are fine)


<?php


$strText = file_get_contents( 'messages\message.txt' );

 

$strFind = '****';

 

$blMatch = false;

 

if( preg_match( "/$strFind/", $strText ) ) {

<blockquote>$blMatch = true;

 

$strText = str_replace( $strFind, '', $strText );</blockquote>}


?>


If you know of any other simple and better php scripts then please let me know, I would also ideally like to be able to remove the whole line with the bad word in!

Edited by Roger, 13 November 2011 - 09:01 AM.
added code tags


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hello, even if the code is working, you do not save the file afterwards for it to show the changes.

Try throwing in a file_put_contents('messages\message.txt', $strText);
PHP: file_put_contents - Manual

Ensure the contents of the string are not empty however, or you may end up replacing the file with something malformed or empty. You should write to a new file and use that for example, keeping the original file just in case.

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
elliottveares

elliottveares

    Newbie

  • Members
  • Pip
  • 5 posts
SORRY FOR THE LATE REPLY!

It ok, I have sorted it now, I modified my other PHP script what gets the contents from the html form and writes them to the text file. Doing it this way was a better was to remove and filter out swear words rather than editing the text file after it had been written.


Here is my new current working PHP script using arrays.


<?php


$newMessage = censor($_GET["Message"]);

$Message = $_GET['Message'];

$space = "\r\n";


$path = "messages/";

$filename = "Message.txt";




$handle = fopen($path.$filename, "a");


function censor($string)

{

 if ($string)

{

//array of swear words

$sweararray = array   ("swearword1", "swearword2", "swearword3");

$replacearray = array ("S*******1", "s*******2", "s*******3");

$newstring = str_ireplace($sweararray, $replacearray, $string);


return $newstring;

}


}


if ($_Post["Message"])

{

  $newMessage = censor($_POST["Message"]);

}


fwrite($handle,$newMessage);



fwrite($handle,$space);



fwrite($handle,$space);



fclose($handle);


header( 'Location: form.html' );



?>


As you can see it now checks for the words in the "sweararray" and replaces them with the corresponding vales in the "replcearray".

Sincerely: Elliott Veares

Edited by Roger, 13 November 2011 - 09:02 AM.
added code tags





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users