Link to home
Start Free TrialLog in
Avatar of cimmer
cimmer

asked on

php convert plural form to singular form

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.
Avatar of KvdnBerg
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.

Avatar of 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-
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
Avatar of cimmer

ASKER

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;
}
ASKER CERTIFIED SOLUTION
Avatar of KvdnBerg
KvdnBerg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial