You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP字符串对比中str_replace失效及similar_text结果异常求助

PHP字符串对比与str_replace失效问题

我正尝试在PHP中对比两个字符串,但不知为何无法正常运行。

<?php
$string1="what is the highest peak in the world";
$string2="The highest peak of World is";
$find_ans=1;
compare_string($string1,$string2,$find_ans);

function compare_string($str1,$str2,$find_ans)
{
    $str1=strtolower($str1);
    $str2=strtolower($str2);
    $skip_word="a,an,the,what,when,where,who,whom,which,whose,why,how,are,am,is,was,were,has,have,had";
    $word_arr=explode(",",$skip_word);
    $new_str1=str_replace($word_arr,'',$str1);
    $new_str2=str_replace($word_arr,'',$str2);
    echo $new_str1.'<br/>';
    echo $new_str2.'<br/>';
    $sim = similar_text($new_str1, $new_str2, $perc);
    echo $perc.'<br/>';
}
?>

使用rsort时,similar_text返回70.769230769231;移除rsort后返回83.333333333333。此处str_replace未生效,$new_str1和$new_str2输出结果与原字符串一致。

更新

我希望通过str_replace处理后,从$string1和$string2得到结果“highest peak world”。但当修改字符串为:

$string1="what is the highest peak in india";
$string2="The highest peak of india is";

得到的结果是“highest peak dia”而非预期的“highest peak India”。

补充说明:$new_str1和$new_str2输出结果与原字符串一致。

内容的提问来源于stack exchange,提问作者Grvito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 19:15:31