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

Posted

技术标签:

【中文标题】<br> 在下一个 foreach 项目中插入空格【英文标题】:<br> inserting space in next foreach item 【发布时间】:2013-08-03 14:26:40 【问题描述】:

我实际上是在做一个拼写检查器。我要做的是按空格将字符串拆分为单词,添加标记以忽略标点符号和nl2br() 函数以保留&lt;br&gt;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);

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

How
How

会输出这个....

How
<br>
 How

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

【问题讨论】:

【参考方案1】:

我怀疑原因在于您的 punctuate() 函数:

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

&lt;br&gt; 后面有个空格。

尝试以下方法:

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

【讨论】:

thxx fr ur answer...但是如果我这样做,它不会拆分它并将其与之前和之后的单词合并...这些空格是为了让拼写检查器认为这是一个词...

以上是关于<br> 在下一个 foreach 项目中插入空格的主要内容,如果未能解决你的问题,请参考以下文章

form表单+EL表达式+过滤器+foreach标签

jstl c:forEach

foreach

如何在markdown中插入换行符<br>

foreach循环只返回数组中的一个项目

数组排序