<br/>在下一个foreach项目中插入空格

时间:2013-08-02 10:34:38

标签: php mysql arrays nl2br

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

spellchecker.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>।</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>।</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);

一切都很好。但它会在下一个单词之后插入一个空格。 此...

How
How

将输出此....

How
<br>
 How

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

1 个答案:

答案 0 :(得分:0)

我怀疑原因在于punctuate()功能:

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

<br>之后有一个空格。

尝试以下方法:

"<br />" => "<br>",
//          ^    ^ Note the lack of spaces.