让PHP的Pspell将修正后的单词作为变量返回

时间:2011-08-26 16:28:11

标签: php spell-checking

我有这个脚本:

<?php
ob_start();
$get = $_GET['q'];
$pspell = pspell_new('en','canadian','','utf-8',PSPELL_FAST);

function spellCheckWord($word) {
    global $pspell;
    $autocorrect = TRUE;
    $word = $word[0];  
    if (preg_match('/^[A-Z]*$/',$word)) return $word;
    if (pspell_check($pspell,$word)) return $word;
    if ($autocorrect && $suggestions = pspell_suggest($pspell,$word))
    return '<u>'.current($suggestions).'</u>';
    return '<b>'.$word.'</b>';
};

function spellCheck($string) {
    return preg_replace_callback('/\b\w+\b/','spellCheckWord',$string);
};

$var = ob_get_clean();
echo $get."<br>";
echo $var;
?>

我希望将更正的字符串放入我的函数变量中。

1 个答案:

答案 0 :(得分:0)

您对PHP语法有一个完全的误解。从函数返回一个字符串不输出字符串。您的所有函数都没有执行任何实际输出(echo,printf等...),因此输出缓冲区无需捕获。同样,这个脚本中没有任何内容需要缓冲,所以这只是一些无用的代码。

您的拼写检查功能也未被执行。所以从本质上讲,除了回显_GET参数外,整个脚本都没有。

<?php

$get = $_GET['q'];

function spellCheckWord($word) {
    $pspell = pspell_new('en','canadian','','utf-8',PSPELL_FAST);
    ...
}

function spellCheck($string) {
    return preg_replace_callback('/\b\w+\b/','spellCheckWord',$string);
}
echo $get, "<br>";
echo spellCheck($word);