duanqu9279 2014-02-04 14:55
浏览 31
已采纳

php函数返回单词而不是字符不起作用

i'm using a php function to return words instead of characters it works fine when i pass string to the function but i have a variable equals another variable containing the string and i've tried the main variable but didn't work

////////////////////////////////////////////////////////
function words($text)
{
$words_in_text = str_word_count($text,1);
$words_to_return = 2;
$result = array_slice($words_in_text,0,$words_to_return);
return '<em>'.implode(" ",$result).'</em>';
}

$intro = $blockRow03['News_Intro'];

echo words($intro); 

/* echo words($blockRow03['News_Intro']);  didn't work either */

the result is nothing

  • 写回答

1条回答 默认 最新

  • drr25281 2014-02-04 15:51
    关注

    str_word_count won't work correctly with accented (multi-byte) characters. you can use below sanitize words function to overcome this problem:

    function sanitize_words($string) {
        preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u",$string,$matches,PREG_PATTERN_ORDER);
        return $matches[0];
    }
    function words($text)
    {
    $words_in_text = sanitize_words($text);
    $words_to_return = 2;
    $result = array_slice($words_in_text,0,$words_to_return);
    return '<em>'.implode(" ",$result).'</em>';
    }
    
    $intro = "aşağı yukarı böyle birşey";
    
    echo words($intro); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线