PHP preg_match找到相关的字 [英] PHP preg_match to find relevant word

查看:200
本文介绍了PHP preg_match找到相关的字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样值的变量:

  $一句话=当谈到时间更新您的汽车保险政策,了解您的运营商如何处理续费;

和我有低于价值数组变量:

  $搜索=阵列('知道','知道','知道','知道');$代替=阵列('意识','自觉','记','专心');

我想找个只是'知道',然后'专心'代替。输出象下面这样:


  

当谈到时间更新您的汽车保险政策,专注于运营商如何处理续签


只搜索'知道'的同义词有关更换而不是别人。感谢您的帮助。

好吧,这里新的code:

  $搜索=阵列('知道','知道','知道','知道');$代替=阵列('自觉','专心','记','意识');

这是一个动态阵列( $搜索),希望大家理解......我们知道,以获得最佳代名词更换使用'知道'以'专心'来代替。输出象下面这样:


  

当谈到时间更新您的汽车保险政策,专注于运营商如何处理续签



解决方案

如何

  $一句话=当谈到时间更新您的汽车保险政策,了解您的运营商如何处理续费;
$搜索=阵列('知道','知道','知道','知道');
$代替=阵列('意识','自觉','记','专心');功能CMP($ A,$ B){
    如果(strpos($ A,$ B)==假的!),返回-1;
    如果(strpos($ B $ A)==假的!),返回1;
    返回0;
}uasort($搜索,'CMP');
$ replaces_new =阵列();
$ I = 0;
的foreach($搜索为$ K => $ V){
    $ replaces_new [$ i] = $替换[$ k]的;
    $ I ++;
}$解析度= str_replace函数($搜索,$ replaces_new,$句);
回声$水库;

输出:

 当谈到时间更新您的汽车保险政策,专注于运营商如何处理续费

I have a variable with value like this :

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

And I have array variables with value below :

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

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

I would like to find just 'be aware of' and then replace with 'concentrate on'. Output like below :

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

Search only 'be aware of' as relevant synonym replacement not others. Thanks for your help.

Ok, here the new code:

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

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

This is a dynamic array ($searches), I hope you understand...and we know to get the best synonym replacement is use ''be aware of' to replace with 'concentrate on'. Output like below :

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

解决方案

How about:

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

output:

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

这篇关于PHP preg_match找到相关的字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆