Solved

php convert plural form to singular form

Posted on 2004-10-26
2,190 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
Question by:cimmer
    5 Comments
     
    LVL 3

    Expert Comment

    by:KvdnBerg
    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
    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
    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
    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:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    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…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    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…

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now