1

我实际上正在做一个拼写检查器。我要做的是按空格将字符串拆分为单词,添加标记以忽略标点符号和nl2br()保留<br>s. 现在,问题是,当我说忽略 时br,它会将其视为一个单词,甚至在下一个单词中添加一个空格。在这里,我将向您展示代码...

拼写检查器.php

function checkspell($string, $translate) {
    $string = $string;
    $counter = 0;
    $arr = explode(' ', punctuate($string, $translate));
    foreach($arr as $str) {
        if (substr($str, 1, 4) == "punc") {
            echo $str;
        } elseif ($str == "<br>") {
            echo "<br />";
        } elseif ($str == "") {
            echo "<punc> </punc>";
        } else {
            $space = "yes";
            if (substr($str, -2, 2) == "<>") {
            $space = "no";
            $str = str_replace("<>", "", $str);
            }
        $exists = mysql_query("SELECT COUNT(word) FROM unicode WHERE word = '$str'") or die (mysql_error());
    if (mysql_result($exists, 0) == 0) {
        $counter++;
        if ($space == "yes") {
            echo "<span class=\"word error\" sug=\"$counter\" space=\"yes\">" . fconvert("Arial Unicode MS", $translate, $str) . " </span><div class=\"suggestions $counter\">";
        } else {
            echo "<span class=\"word error\" sug=\"$counter\" space=\"yes\">" . fconvert("Arial Unicode MS", $translate, $str) . "</span><div class=\"suggestions $counter\">";
        }
        echo "<div id=\"sugwrds$counter\"><i>Loading suggestions...</i></div><hr size=\"1\" color=\"#ccc\"><span class=\"ignore\" idt=\"$counter\">Ignore</span><span class=\"ignoreall\" idt=\"$counter\">Ignore All</span><hr size=\"1\" color=\"#ccc\"><span class=\"suggdiswrd\" href=\"dialog/suggest?word=$str\" idt=\"$counter\" gur=\"box\">Suggest this word</span></div>";
    } else {
        $note = mysql_query("SELECT note FROM unicode WHERE word = '$str'");
        if (mysql_result($note, 0) == "") {
            if ($space == "yes") {
                echo "<span class=\"whps\">" . fconvert("Arial Unicode MS", $translate, $str) . " </span>";
            } else {
                echo "<span class=\"whps\">" . fconvert("Arial Unicode MS", $translate, $str) . "</span>";
            }
        } else {
            if ($space == "yes") {
                echo "<span class=\"whps blue\">" . fconvert("Arial Unicode MS", $translate, $str) . " </span>";
            } else {
                echo "<span class=\"whps blue\">" . fconvert("Arial Unicode MS", $translate, $str) . "</span>";
            }
            }
        }
    }
}
}

punctuation.php(在 br 之前插入一个空格,这样它就不会与任何单词合并)

function punctuate($pstr, $font) {
    $repp = array(
        "  " => " ",
        "<br />" => " <br> ",
        " ।" => " <punc>।&lt;/punc> ",
        " ," => " <punc>,</punc> ",
        " ." => " <punc>.</punc> ",
        " /" => " <punc>/</punc> ",
        " \\" => " <punc>\\</punc> ",
        " !" => " <punc>!</punc> ",
        " ?" => " <punc>?</punc> ",
        " :" => " <punc>:</punc> ",
        " ;" => " <punc>;</punc> ",
        " \"" => " <punc>\"</punc> ",
        " '" => " <punc>'</punc> ",
        " (" => " <punc>(</punc> ",
        " )" => " <punc>)</punc> ",
        " {" => " <punc>{</punc> ",
        " }" => " <punc>}</punc> ",
        " [" => " <punc>[</punc> ",
        " ]" => " <punc>]</punc> ",
        " <" => " <punc><</punc> ",
        " >" => " <punc>></punc> ",
        " &" => " <punc>&</punc> ",
        " $" => " <punc>$</punc> ",
        " @" => " <punc>@</punc> ",
        " #" => " <punc>#</punc> ",
        " %" => " <punc>%</punc> ",
        " ^" => " <punc>^</punc> ",
        " *" => " <punc>*</punc> ",
        " _" => " <punc>_</punc> ",
        " =" => " <punc>=</punc> ",
        " +" => " <punc>+</punc> ",
        " |" => " <punc>|</punc> ",
        " -" => " <punc>-</punc> ",
        " 1" => " <punc>1</punc> ",
        " 2" => " <punc>2</punc> ",
        " 3" => " <punc>3</punc> ",
        " 4" => " <punc>4</punc> ",
        " 5" => " <punc>5</punc> ",
        " 6" => " <punc>6</punc> ",
        " 7" => " <punc>7</punc> ",
        " 8" => " <punc>8</punc> ",
        " 9" => " <punc>9</punc> ",
        " 0" => " <punc>0</punc> ",
        "।" => "<> <punc>।&lt;/punc> ",
        "," => "<> <punc>,</punc> ",
        "." => "<> <punc>.</punc> ",
        "/" => "<> <punc>/</punc> ",
        "\\" => "<> <punc>\\</punc> ",
        "!" => "<> <punc>!</punc> ",
        "?" => "<> <punc>?</punc> ",
        ":" => "<> <punc>:</punc> ",
        ";" => "<> <punc>;</punc> ",
        "\"" => "<> <punc>\"</punc> ",
        "'" => "<> <punc>'</punc> ",
        "(" => "<> <punc>(</punc> ",
        ")" => "<> <punc>)</punc> ",
        "{" => "<> <punc>{</punc> ",
        "}" => "<> <punc>}</punc> ",
        "[" => "<> <punc>[</punc> ",
        "]" => "<> <punc>]</punc> ",
        "<" => "<> <punc><</punc> ",
        ">" => "<> <punc>></punc> ",
        "&" => "<> <punc>&</punc> ",
        "$" => "<> <punc>$</punc> ",
        "@" => "<> <punc>@</punc> ",
        "#" => "<> <punc>#</punc> ",
        "%" => "<> <punc>%</punc> ",
        "^" => "<> <punc>^</punc> ",
        "*" => "<> <punc>*</punc> ",
        "_" => "<> <punc>_</punc> ",
        "=" => "<> <punc>=</punc> ",
        "+" => "<> <punc>+</punc> ",
        "|" => "<> <punc>|</punc> ",
        "1" => "<> <punc>1</punc> ",
        "2" => "<> <punc>2</punc> ",
        "3" => "<> <punc>3</punc> ",
        "4" => "<> <punc>4</punc> ",
        "5" => "<> <punc>5</punc> ",
        "6" => "<> <punc>6</punc> ",
        "7" => "<> <punc>7</punc> ",
        "8" => "<> <punc>8</punc> ",
        "9" => "<> <punc>9</punc> ",
        "0" => "<> <punc>0</punc> ",
    );
    $repps = strtr($pstr, $repp);
    if ($font !== "DrChatrikWeb") {
    $reppsq = array(

    );
    $repps = strtr($repps, $reppsq);
    }
    return $repps;
}

这就是函数的执行方式...

$string = nl2br($_POST['string']);
$translate = $_POST['translate'];
checkspell($string, $translate);

一切正常。但它会在 br 之后的下一个单词中插入一个空格……这……

How
How

会输出这个....

How
<br>
 How

第二个中有一个空格,how文本框中没有。

4

1 回答 1

0

I suspect the cause is in your punctuate() function:

"<br />" => " <br> ",

There's a space there after the <br>.

Try the following:

"<br />" => "<br>",
//          ^    ^ Note the lack of spaces.
于 2013-08-02T10:40:43.803 回答