gpt4 book ai didi

PHP preg_match 查找相关单词

转载 作者:行者123 更新时间:2023-12-04 05:59:59 29 4
gpt4 key购买 nike

我有一个像这样值的变量:

$sentence = "When it comes time to renew your auto insurance policy, be aware of how your carrier handles renewals";

我有以下值的数组变量:
$searches = array('aware', 'aware of', 'be aware', 'be aware of');

$replaces = array('conscious', 'conscious of', 'remember', 'concentrate on');

我想找到只是“意识到”,然后替换为“专注”。输出如下:

When it comes time to renew your auto insurance policy, concentrate on how your carrier handles renewals



仅搜索“注意”作为相关同义词替换而不是其他同义词。谢谢你的帮助。

好的,这里是新代码:
$searches = array('aware of', 'be aware of', 'be aware', 'aware');

$replaces = array('conscious of', 'concentrate on', 'remember', 'conscious');

这是一个动态数组( $searches ),我希望你理解......我们知道最好的同义词替换是使用“注意”替换为“专注”。输出如下:

When it comes time to renew your auto insurance policy, concentrate on how your carrier handles renewals

最佳答案

怎么样:

$sentence = "When it comes time to renew your auto insurance policy, be aware of how your carrier handles renewals";
$searches = array('aware', 'aware of', 'be aware', 'be aware of');
$replaces = array('conscious', 'conscious of', 'remember', 'concentrate on');

function cmp($a, $b) {
if (strpos($a, $b) !== false) return -1;
if (strpos($b, $a) !== false) return 1;
return 0;
}

uasort($searches, 'cmp');
$replaces_new = array();
$i=0;
foreach($searches as $k=>$v) {
$replaces_new[$i] = $replaces[$k];
$i++;
}

$res = str_replace($searches, $replaces_new, $sentence);
echo $res;

输出:
When it comes time to renew your auto insurance policy, concentrate on how your carrier handles renewals

关于PHP preg_match 查找相关单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9031199/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com