Xio Jin
Xio Jin

Reputation: 23

PHP preg_match to find relevant word

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

Upvotes: 2

Views: 649

Answers (4)

Toto
Toto

Reputation: 91415

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

Upvotes: 0

Tim S.
Tim S.

Reputation: 13843

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.

Upvotes: 1

Eugen Rieck
Eugen Rieck

Reputation: 65274

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

Upvotes: 1

Rikesh
Rikesh

Reputation: 26421

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)

Upvotes: 1

Related Questions