[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

replace similar words in array

Posted on 2009-02-16
8
Medium Priority
?
711 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
0
Comment
Question by:Pedro Chagas
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:atannus
ID: 23648827
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 23649139
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
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23649148
Why is the first word in the array "shoes" not replaced by "shoe" in the result array?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Pedro Chagas
ID: 23649181
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 23649513
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
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 23649876
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 23650045
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
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 2000 total points
ID: 23650134
The input array should be named $input.

After line 4 in the snippet, add this:

$input = $word;
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question