RE: [PHP] Question about shortening a string
-Original Message- From: Steve Slater [mailto:[EMAIL PROTECTED] Sent: Saturday, March 12, 2005 5:29 PM $name_array = explode( ,$name); $short_name = $name_array[0] . end($name_array); And a dozen other ways to do it I'm sure. I just finished a project with a mess of pattern matching so have regex on the brain. :-) Hehe, I know the feeling Ereg solution: eregi(^([a-z] ).* ?([a-z])$, $name, $regs); print the name in short is $regs[1] $regs[2]; But when dealing with names I think Your approach is better since there´s all kinds of chararcters from language to language, in Denmark ÆæØøÅå, in sweden äÄöÖ and so on. Of course I could do something like: ereg(^(.+ ).* ?(.+)$, $name, $regs); // untested -- Med venlig hilsen / best regards ComX Networks A/S Kim Madsen Systemudvikler Naverland 31 DK-2600 Glostrup www.comx.dk Telefon: +45 70 25 74 74 Telefax: +45 70 25 73 74 E-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Question about shortening a string
At 11:54 PM 3/11/2005, Stephen Johnson wrote: Just a quick and dirty solution - but couldn't you explode the name into an array and use the first and last array fields ? $name_array = explode( ,$name); $short_name = $name_array[0] . end($name_array); And a dozen other ways to do it I'm sure. I just finished a project with a mess of pattern matching so have regex on the brain. :-) Steve On Mar 11, 2005, at 11:39 PM, Steve Slater wrote: Mario, Many ways to do it, most using regex. This is not as clean as it can be but still shows the idea: ?php $name = Mario Augusto Machado dos Reis Gamito; preg_match (/^(\w+)(\W*)(\w+\W+)*(\w+)$/,$name,$matches); $short_name = $matches[1] $matches[4]; ? This example works for any number of middle names (including zero) and any number of white spaces between words. You can probably get this more elegant, but this is one idea. Steve In Portgal we have big names. My complete name, for instance, is Mário Augusto Machado dos Reis Gamito. Mário is the Christian name and Gamito - the last one - is always the last name of the father. I have a form where i want to let my users insert their full big name like mine, but i want to shorten them just to the first and the last. For example, if i fill the form with Mário Augusto Machado dos Reis Gamito, i want to change this string to only Mário Gamito. The total number of names is not fix. Could be 3, 4, 5, 6,... My son name is Manuel Ramos Gamito. Only 3 names. Should be changed to Manuel Gamito, also. I've looked at the strings functions in the manual, made a lot of experiences, but couldn't get there :( Does anyone knows how to do this ? Any help would be apreciated. Warm Regards, Mário Gamito -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Steve Slater [EMAIL PROTECTED] PHP / MySQL / Web App Security (LAMP) Training: http://www.handsonsecurity.com/training.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php * Stephen Johnson [EMAIL PROTECTED] http://www.thelonecoder.com --continuing the struggle against bad code-- * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Steve Slater [EMAIL PROTECTED] PHP / MySQL / Web App Security (LAMP) Training: http://www.handsonsecurity.com/training.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Question about shortening a string
Hi, In Portgal we have big names. My complete name, for instance, is Mário Augusto Machado dos Reis Gamito. Mário is the Christian name and Gamito - the last one - is always the last name of the father. I have a form where i want to let my users insert their full big name like mine, but i want to shorten them just to the first and the last. For example, if i fill the form with Mário Augusto Machado dos Reis Gamito, i want to change this string to only Mário Gamito. The total number of names is not fix. Could be 3, 4, 5, 6,... My son name is Manuel Ramos Gamito. Only 3 names. Should be changed to Manuel Gamito, also. I've looked at the strings functions in the manual, made a lot of experiences, but couldn't get there :( Does anyone knows how to do this ? Any help would be apreciated. Warm Regards, Mário Gamito -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Question about shortening a string
Mario, Many ways to do it, most using regex. This is not as clean as it can be but still shows the idea: ?php $name = Mario Augusto Machado dos Reis Gamito; preg_match (/^(\w+)(\W*)(\w+\W+)*(\w+)$/,$name,$matches); $short_name = $matches[1] $matches[4]; ? This example works for any number of middle names (including zero) and any number of white spaces between words. You can probably get this more elegant, but this is one idea. Steve In Portgal we have big names. My complete name, for instance, is Mário Augusto Machado dos Reis Gamito. Mário is the Christian name and Gamito - the last one - is always the last name of the father. I have a form where i want to let my users insert their full big name like mine, but i want to shorten them just to the first and the last. For example, if i fill the form with Mário Augusto Machado dos Reis Gamito, i want to change this string to only Mário Gamito. The total number of names is not fix. Could be 3, 4, 5, 6,... My son name is Manuel Ramos Gamito. Only 3 names. Should be changed to Manuel Gamito, also. I've looked at the strings functions in the manual, made a lot of experiences, but couldn't get there :( Does anyone knows how to do this ? Any help would be apreciated. Warm Regards, Mário Gamito -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Steve Slater [EMAIL PROTECTED] PHP / MySQL / Web App Security (LAMP) Training: http://www.handsonsecurity.com/training.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Question about shortening a string
Just a quick and dirty solution - but couldn't you explode the name into an array and use the first and last array fields ? On Mar 11, 2005, at 11:39 PM, Steve Slater wrote: Mario, Many ways to do it, most using regex. This is not as clean as it can be but still shows the idea: ?php $name = Mario Augusto Machado dos Reis Gamito; preg_match (/^(\w+)(\W*)(\w+\W+)*(\w+)$/,$name,$matches); $short_name = $matches[1] $matches[4]; ? This example works for any number of middle names (including zero) and any number of white spaces between words. You can probably get this more elegant, but this is one idea. Steve In Portgal we have big names. My complete name, for instance, is Mário Augusto Machado dos Reis Gamito. Mário is the Christian name and Gamito - the last one - is always the last name of the father. I have a form where i want to let my users insert their full big name like mine, but i want to shorten them just to the first and the last. For example, if i fill the form with Mário Augusto Machado dos Reis Gamito, i want to change this string to only Mário Gamito. The total number of names is not fix. Could be 3, 4, 5, 6,... My son name is Manuel Ramos Gamito. Only 3 names. Should be changed to Manuel Gamito, also. I've looked at the strings functions in the manual, made a lot of experiences, but couldn't get there :( Does anyone knows how to do this ? Any help would be apreciated. Warm Regards, Mário Gamito -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Steve Slater [EMAIL PROTECTED] PHP / MySQL / Web App Security (LAMP) Training: http://www.handsonsecurity.com/training.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php * Stephen Johnson [EMAIL PROTECTED] http://www.thelonecoder.com --continuing the struggle against bad code-- * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php