Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php convert plural form to singular form

Posted on 2004-10-26
5
Medium Priority
?
2,456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

636 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