开发者

Getting PHP's Pspell to return corrected word as a variable

开发者 https://www.devze.com 2023-03-31 14:58 出处:网络
I have this script: <?php ob_start(); $get = $_GET[\'q\']; $pspell = pspell_new(\'en\',\'canadian\',\'\',\'utf-8\',PSPELL_FAST);

I have this script:

<?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;
?>

I want the corrected string put开发者_StackOverflow中文版 into a variable from my function.


You've got a complete misunderstanding of PHP syntax. returning a string from a function does NOT output the string. None of your functions do any actual output (echo, printf, etc...) so there is NOTHING for the output buffer to capture. AS well, there is nothing in this script which would require buffering to take place, so that's just some useless code.

Your spell check functions are also NOT being executed. So in essence, this entire script does NOTHING except echo out a _GET parameter.

<?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);
0

精彩评论

暂无评论...
验证码 换一张
取 消