1) 다른 텍스트 파일 (unwanted-words.txt)과 하나의 입력 단어가 일치하지 않는 경우에만 입력 텍스트를 파일 (text.txt)에 쓰는 php 함수를 실행하는 방법
<?php
$para = $_POST['para'];
if ($_POST)
{
$handle = fopen("text.txt", "a");
fwrite($handle, $para . ":<br/>" );
fclose($handle);
}
?>
<form method="post">
Para:<input type="text" name="para"><br/>
<input type="submit" name="submit" value="Post">
</form>
2) 또한, wanted-words.txt 파일에서 원하지 않는 단어에 어떤 형식을 사용해야합니까?
badword,bad word,bad-word,bad_word,bad.word
또는
badword
bad word
bad-word
bad_word
bad.word
또는 다른 형식
미리 감사드립니다
이것을 시도 할 수 있습니다.
Text.php의 클래스 텍스트
<?php
/**
* Text Class
*/
class Text
{
//text string
private $text;
public function __construct($text)
{
//set value for $this->text for each objects/instances
$this->text = $text;
}
//filter with a "filter file"
public function filterFile($filter)
{
//get unwanted words file content :D
$filter = file_get_contents($filter);
//explode string every end of line for getting an array
$filter = explode(PHP_EOL, $filter);
foreach ($filter as $v) {
if(preg_match("/$v/i", $this->text)){
$this->text = "";
}
}
//return object for succesive methods (ex: $ex->a()->b()->c() )
return $this;
}
//save modified string in file
//first param => file name
public function save($filename)
{
//set handle
$handle = fopen($filename, 'a');
//if true
if($handle)
{
//write file
fwrite($handle, $this->text.PHP_EOL);
} else {
return false;
}
//close handle
fclose($handle);
}
}
txt 파일의 원하지 않는 단어 : 예를 들어 원하지 않는 -words.txt
badword
bad word
bad-word
bad_word
bad.word
나는 당신의 페이지 ...
<?php
require "Text.php";
if($_POST["para"])
{
//new instance of Text Class
$text = new Text($_POST["para"]);
$text->filterFile('unwanted-words.txt')->save("test.txt");
}
?>
<form method="post">
Para:<input type="text" name="para"><br/>
<input type="submit" name="submit" value="Post">
</form>
제출시 개미없는 단어가 일치하지 않는 경우에만 텍스트가 파일에 추가됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다