2013년 12월 13일 금요일

PHP 단어 수 세기 우선순위 버전

1. index.php

<?php
include "wordCheck.php";
if (isset($_POST['content']))
{
    $wordCheck = new WordCheck;
    $content = stripslashes($_POST['content']);
    $exploded = $wordCheck->MultiExplode($content);
    $wordList = $wordCheck->GetWordList($exploded);
    $count = (count($wordList,1)/count($wordList,0))-1;
}

?>

<?php echo "Input " ?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
    <textarea rows="25" cols="60" name="content"></textarea>
    <input type="submit" value="Save">
</form>


<textarea rows="35" cols="60" name="result">
<?php for($i=0; $i < $count; $i++)
{
    echo "Word : " . $wordList[0][$i] . "    " .
         "Count : " . $wordList[1][$i] . "\n";
}
?></textarea>

<?php for($i=0; $i < $count; $i++)
{
    echo "<a charset=UTF-8 href=http://endic.naver.com/search.nhn?sLn=kr&searchOption=all&query="
    . $wordList[0][$i] . ">" . $wordList[0][$i] . "</a>" . "\n";
}
?>



2. wordCheck

<?php
class WordCheck
{
    var $delimiters = array('\"', '?', '<', '>', '(', ')',
                         '!', '-', '/', ',', '.', '|',
                          '=', ':', ' ', "\r\n", "\t");

    function MultiExplode($string)
    {
        $ready = str_replace($this->delimiters, $this->delimiters[0], $string);
        $launch = explode($this->delimiters[0], $ready);
        return  $launch;
    }
    function GetWordList($exploded)
    {
        $count = array();
        $word = array();
        $unique = (array_unique($exploded));
        foreach($unique as $token)
        {
            if (strcmp("\r\n", $token) == -1 || strcmp(" ", $token) == -1)
            {
                $countNum = count(array_keys($exploded, $token));
                array_push($count, $countNum);
                array_push($word, $token);
            }
        }
        array_multisort($count, SORT_DESC,SORT_NUMERIC, $word, SORT_ASC, SORT_STRING);
        return array($word, $count);
    }
}
?>



댓글 없음:

댓글 쓰기