?
Solved

php convert plural form to singular form

Posted on 2004-10-26
5
Medium Priority
?
2,550 Views
Last Modified: 2012-06-27
I dont think this is possible or that it can be easily done. I may be wrong.
Is it possible to write a PHP function that would convert a string from the "plural" form to the "singular" form??

Like convert Murals to Mural
or "Toys" to "Toy"
or even "Babies" to "Baby".
and maybe even "boxes" to "box"

If there is an easier way, enlighten me, otherwise I was thinking to write a function to to do the following...
how could this be done?

1) Look at the last 2 letters in word, if last letter is "S" and 2nd to last letter is NOT "S", trim off "S"
2) Look at end of word for "VES", if found, replace with "F"
3) Look at end of word for "OES", if found, replace with "O"
4) Look at end of word for "OS", if found, replace with "O"
5) Look at end of word for "IES", if found, replace with "Y"
6) Look at end of word for "XES", if found, replace with "X"

All other plural forms I am not concerned with as they are less common.
0
Comment
Question by:cimmer
  • 3
5 Comments
 
LVL 3

Expert Comment

by:KvdnBerg
ID: 12411170
Assuming you're using PHP4 (not 5 which would make this easier), this is a way to do it:

for 1)

$endpos = strlen($string) - 1;
if (strrpos($string, "s")==$endpos) && strrpos(substr($string, $endpos-1, $endpos) == "s")  {
    $string_singular = substr($string, 0, $endpos);
}

for the others:

$endpos_three = strlen($string) - 3;

if(substr($string, $endpos_three, 3) == $plural_end)) { //where $plural_end is "ves" or "oes" etc  
    $string_singular = substr($string, $endpos_three) . $replace_singular; //where $replace_singular, is f, o, etc
}

I haven't fully tested it so if it doesn't work properly, let me know.

0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12411182
Well you could always write a function in somewhat a template like fashion:

<?php
$sentence = 'In our records you have {one baby|multiple babies}';
$select = 1; //1 is single, 2 is plural//

function tpl_select($sentence, $option=1)
{
  if(preg_match_all('%\{([^}]+)\}%',$sentence, $blocks))
  {
    foreach($blocks as $block)
    {
      $b = $block[0];
      $b = str_replace(array('{','}'), '', $b);
      $selects = explode('|',$b);
      $select = isset($selects[$option-1]) ? $selects[$option-1] : $selects[0];
     
      $sentence = str_replace('{'.$b.'}',$select, $sentence);
    }
  }
  return $sentence;
}

echo '<div>'.tpl_select($sentence, 1).'</div>';
echo '<div>'.tpl_select($sentence, 2).'</div>';
?>

Regards

-r-
0
 
LVL 3

Expert Comment

by:KvdnBerg
ID: 12411198
BTW I forgot, PHP5 makes it easier cause you could use strrpos with more than one character (php4 only works with one character), so you could use strrpos to check the end of the string
0
 

Author Comment

by:cimmer
ID: 12411986
KvdnBerg, if you want to copy my answer below into a new post I'll give you the points since you helped me figure it out.

heres what I did... (these are just some of the many grammer rules)


//reverse some plural forms to singular
function trimplural($string){

      
      $plural_end='os';
      $replace_singular='o';
      if(      substr($string, -2) == $plural_end) {
            $string = substr($string, 0,strlen($string)-2). $replace_singular;
      }
      

      $plural_end='ies';
      $replace_singular='y';
      if(      substr($string, -3) == $plural_end) {
            $string = substr($string, 0,strlen($string)-3). $replace_singular;
      }
      
      
      $plural_end='xes';
      $replace_singular='x';
      if(      substr($string, -3) == $plural_end) {
            $string = substr($string, 0,strlen($string)-3). $replace_singular;
      }
      
      $plural_end='oes';
      $replace_singular='o';
      if(      substr($string, -3) == $plural_end) {
            $string = substr($string, 0,strlen($string)-3). $replace_singular;
      }
      
      $plural_end='ies';
      $replace_singular='y';
      if(      substr($string, -3) == $plural_end) {
            $string = substr($string, 0,strlen($string)-3). $replace_singular;
      }
      
      
      $plural_end='ves';
      $replace_singular='f';
      if(      substr($string, -3) == $plural_end) {
            $string = substr($string, 0,strlen($string)-3). $replace_singular;
      }
      
      $plural_end='s';
      $replace_singular='';
      if(      substr($string, -1) == $plural_end && !(substr($string, -2) == 'ss') ) {    
            $string = substr($string, 0,strlen($string)-1). $replace_singular;
      }
      return $string;
}
0
 
LVL 3

Accepted Solution

by:
KvdnBerg earned 1996 total points
ID: 12412027
Ok, this is your solution with my suggestions

//reverse some plural forms to singular
function trimplural($string){

     
     $plural_end='os';
     $replace_singular='o';
     if(     substr($string, -2) == $plural_end) {
          $string = substr($string, 0,strlen($string)-2). $replace_singular;
     }
     

     $plural_end='ies';
     $replace_singular='y';
     if(     substr($string, -3) == $plural_end) {
          $string = substr($string, 0,strlen($string)-3). $replace_singular;
     }
     
     
     $plural_end='xes';
     $replace_singular='x';
     if(     substr($string, -3) == $plural_end) {
          $string = substr($string, 0,strlen($string)-3). $replace_singular;
     }
     
     $plural_end='oes';
     $replace_singular='o';
     if(     substr($string, -3) == $plural_end) {
          $string = substr($string, 0,strlen($string)-3). $replace_singular;
     }
     
     $plural_end='ies';
     $replace_singular='y';
     if(     substr($string, -3) == $plural_end) {
          $string = substr($string, 0,strlen($string)-3). $replace_singular;
     }
     
     
     $plural_end='ves';
     $replace_singular='f';
     if(     substr($string, -3) == $plural_end) {
          $string = substr($string, 0,strlen($string)-3). $replace_singular;
     }
     
     $plural_end='s';
     $replace_singular='';
     if(     substr($string, -1) == $plural_end && !(substr($string, -2) == 'ss') ) {    
          $string = substr($string, 0,strlen($string)-1). $replace_singular;
     }
     return $string;
}
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

599 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