【问题标题】:PHP preg_match to find relevant wordPHP preg_match 查找相关单词
【发布时间】:2012-01-27 09:25:07
【问题描述】:

我有一个值这样的变量:

$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');

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

当需要更新您的汽车保险单时,请专注于您的承运人如何处理更新

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

好的,这里是新代码:

$searches = array('aware of', 'be aware of', 'be aware', 'aware');

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

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

当需要更新您的汽车保险单时,请专注于您的承运人如何处理更新

【问题讨论】:

  • 按字符串长度对搜索模式进行排序。并且可能在它们周围使用\b 锚。
  • 请在php中显示代码?

标签: php arrays string preg-match str-replace


【解决方案1】:

我假设您的搜索和替换数组是静态的。

试试这个,

str_replace($searches[3],$replaces[3],$sentence)

还可以通过以下方式替换特定的“提防”:

str_replace("%be aware of%","concentrate on",$sentence)

【讨论】:

  • 请注意,我只需要替换 $searches 中具有相同单词的相关同义词。从哪里开始最大匹配不是问题。
  • 是的,所以在这种情况下,只需使用str_replace($searches, $replaces, $subject);
  • 只需从 $searches 中搜索匹配同义词来替换,我们知道真正的同义词是“注意”。怎么做?
  • 这是动态替换变量搜索以搜索相关性同义词。我想你知道我的意思.. 尝试从 $searches 中找出 $sentence 中的匹配方式,而不仅仅是通过 simple_replace 重新排序来重新排序。
【解决方案2】:

这里不需要正则表达式,

首先以某种方式对常量数组进行排序,使其首先找到最大的匹配项:

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

然后使用 str_replace

$newsentence=str_replace($searches,$replaces, $sentence);

【讨论】:

  • 请注意,我只需要替换 $searches 中具有相同单词的相关同义词。从哪里开始最大匹配不是问题。
  • @XioJin 这就是我重新排序$replaces的原因,我重新排序$searches
  • $searches 是动态数组,您不能对这些值进行排序。只需搜索匹配的同义词进行替换,我们就知道真正的同义词是“注意”
  • @XioJin 只要保证 $searches 和 $replaces 的顺序正确(对于所有有效的 $i,$searches[$i] 对应 $replaces[$i])你没问题。
  • 是的..我知道!但这是将变量搜索动态替换为搜索相关性同义词。我想你知道我的意思.. 尝试从 $searches 中找出 $sentence 中的匹配方式,而不仅仅是通过 simple_replace 重新排序来重新排序。
【解决方案3】:

如果你改变你的搜索顺序,使得第一个元素不能匹配数组后面的元素,你可以正常使用str_replace($searches, $replaces, $subject);

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

如果字符串包含“beware”,“beware of”将不匹配,而“beware”将匹配。如果你有相反的顺序,“aware”将匹配“beware”,这是错误的。

【讨论】:

  • 请注意,我只需要替换 $searches 中具有相同单词的相关同义词。从哪里开始最大匹配不是问题。
  • @XioJin 你从哪里得到搜索和替换?如果它来自 SQL,您可以使用 SQL 进行排序,这会更容易......
【解决方案4】:

怎么样:

$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

【讨论】:

  • 感谢 M42。对我来说完美的解决方案! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多