PHP preg_match to find relevant word

笑着哭i 提交于 2019-12-24 00:03:32

问题


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


回答1:


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



回答2:


I assume that your search & replace array are static.

Try this,

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

Also to just replace specific "beware of" you can do simply by:

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




回答3:


No need for a regexp here,

First sort your constant array in a way, that it finds the biggest match first:

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

then use str_replace

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



回答4:


If you change the order of your searches so that the first element can not match an element later in the array, you can use str_replace($searches, $replaces, $subject); normally!

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

If the string contains "be aware", "be aware of" will not match and "be aware" will. If you'd have the opposite order, "aware" would match "be aware" which would be wrong.



来源:https://stackoverflow.com/questions/9031199/php-preg-match-to-find-relevant-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!