We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

replace similar words in array

Pedro Chagas
Pedro Chagas asked
on
Medium Priority
739 Views
Last Modified: 2012-05-06
Hi, I have a array that contain words, and some words are similar.
So, if in array I have values like shoe or shoes, I want replace shoes for shoe:
I have this array:
Array
(
    [0] => shoes
    [1] => glove
    [2] => gloves
    [3] => ties
    [4] => bracelets
    [5] => gloves
    [6] => shoe
    [7] => shoes
)
and I want:

Array
(
    [0] => shoes
    [1] => glove
    [2] => glove
    [3] => ties
    [4] => bracelets
    [5] => glove
    [6] => shoe
    [7] => shoe
)

First the script have to detect if have words like shoe and shoes (similar words), and if have,  the word shoes are replace by shoe (is just detect the 's').

How I do this?

Regards,
JC
Comment
Watch Question

Commented:
You need to perform two steps:

1. Tell PHP what (shoes) should be replaced by what (shoe).

One interesting way of doing this is by creating an instruction array, like so:

array (
[shoe ]=> shoes
[shirt] => shirts
[pant] => pants
)

Then, you loop into this array, and for every $key => $value pair, you search the target array for $value, replacing it with $key.

It is even possible that every key in the instruction array holds another array, with ALL the words that sould be replaced with shoe (shoes, shoo, shows).

Does this help?
Pedro ChagasWebmaster

Author

Commented:
Yes this help, and I understand your idea. The problem is I don't know work with arrays. If I know working with arrays, maybe I get the solution in this time.
Can be more precise?

Regards,
JC
Top Expert 2008

Commented:
Why is the first word in the array "shoes" not replaced by "shoe" in the result array?
Pedro ChagasWebmaster

Author

Commented:
Hi, I´m sorry, the correct is:
Array
(
    [0] => shoe //change
    [1] => glove
    [2] => glove
    [3] => ties
    [4] => bracelets
    [5] => glove
    [6] => shoe
    [7] => shoe
)
Pedro ChagasWebmaster

Author

Commented:
If I have the array $words that contain this values:
Array
(
    [0] => shoes
    [1] => glove
    [2] => gloves
    [3] => ties
    [4] => bracelets
    [5] => gloves
    [6] => shoe
    [7] => shoes
)

How I do for get this solution:
(
    [0] => shoe //change
    [1] => glove
    [2] => glove
    [3] => ties
    [4] => bracelets
    [5] => glove
    [6] => shoe
    [7] => shoe
)

Regards, JC
Top Expert 2008
Commented:
Try this approach:

1) collect all unique words
2) determine which words are plural version of existing singular word
# 1) collect all unique words
$all_words = array();
foreach($input as  $word) {
  if(in_array($word,$all_words)) continue;
  $all_words[] = $word;
}
# 2) determine which words are plural version of existing singular word
$result = array();
foreach($input as  $word) {
  if((substr($word,strlen($word)-1,1)=='s') # ends with 's'
    and in_array(substr($word,0,strlen($word)-1),$all_words)) # exists as singular
    $result[] = substr($word,0,strlen($word)-1);  # change to singular form
  else 
    $result[] = $word;
}
print_r($result);

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Pedro ChagasWebmaster

Author

Commented:
Hi, I try to test the code of 'cxr' but I get this warning:
Warning: Invalid argument supplied for foreach() in /home/tags/public_html/bastidores/teste.php on line 15

Warning: Invalid argument supplied for foreach() in /home/tags/public_html/bastidores/teste.php on line 21

The only changes I do in the code of 'cxr' is create teh array word!

Please view what happening with the code.

Regards, jc
<?
$palavras = "shoe shoes candle gloves glove glove";
$word = explode(' ', $palavras);
print_r($word);
# 1) collect all unique words
$all_words = array();
foreach($input as  $word) { //line15
  if(in_array($word,$all_words)) continue;
  $all_words[] = $word;
}
# 2) determine which words are plural version of existing singular word
$result = array();
foreach($input as  $word) { //line21
  if((substr($word,strlen($word)-1,1)=='s') # ends with 's'
    and in_array(substr($word,0,strlen($word)-1),$all_words)) # exists as singular
    $result[] = substr($word,0,strlen($word)-1);  # change to singular form
  else 
    $result[] = $word;
}
print_r($result);
?>

Open in new window

Top Expert 2008
Commented:
The input array should be named $input.

After line 4 in the snippet, add this:

$input = $word;
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.