입력 단어가 텍스트 파일에서 원하지 않는 단어와 일치하지 않는 경우에만 PHP 실행 쓰기 기능

희미한

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관