• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 723
  • Last Modified:

replace similar words in array

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
0
Pedro Chagas
Asked:
Pedro Chagas
  • 4
  • 3
2 Solutions
 
atannusCommented:
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?
0
 
Pedro ChagasWebmasterAuthor 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
0
 
Roger BaklundCommented:
Why is the first word in the array "shoes" not replaced by "shoe" in the result array?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Pedro ChagasWebmasterAuthor 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
)
0
 
Pedro ChagasWebmasterAuthor 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
0
 
Roger BaklundCommented:
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

0
 
Pedro ChagasWebmasterAuthor 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

0
 
Roger BaklundCommented:
The input array should be named $input.

After line 4 in the snippet, add this:

$input = $word;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now