RE: [PHP] First Character In A String
$firstchar = substr($string, 0, 1); Read www.php.net/substr Best regards, Peter Lauri www.dwsasia.com - company web site www.lauri.se - personal web site www.carbonfree.org.uk - become Carbon Free -Original Message- From: Christopher Deeley [mailto:[EMAIL PROTECTED] Sent: Sunday, January 21, 2007 3:44 PM To: php-general@lists.php.net Subject: [PHP] First Character In A String Can anyone tell me if there is a function to return the first letter in a string, such as: $surname=SMITH; $forename=ALAN; Is there a function which I can use to make $forename A, so I can display it as A SMITH? Thank You In Advance -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] First Character In A String
Hello Christopher Deeley, Best regards, === At 2007-01-21, 21:49:06 you wrote: === Can anyone tell me if there is a function to return the first letter in a string, such as: $surname=SMITH; $forename=ALAN; Is there a function which I can use to make $forename A, so I can display it as A SMITH? $forename[0].' '.$surname Thank You In Advance just like an array = = = = = = = = = = = = = = = = = = = = Nicholas Yim [EMAIL PROTECTED] 2007-01-21 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] First Character In A String
Christopher Deeley wrote: Can anyone tell me if there is a function to return the first letter in a string, such as: $surname=SMITH; $forename=ALAN; Is there a function which I can use to make $forename A, so I can display it as A SMITH? another alternative to the other answers you have had, this alternative introduces you to the 'String access and modification by character' functionality as described here: http://nl2.php.net/manual/en/language.types.string.php ?php $initial = (is_string($forename) strlen($forename) 0) ? $forename[0] : '' ; echo trim($initial.' '.$surname); Thank You In Advance -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] First Character In A String
On Sun, 2007-01-21 at 16:02 +0100, Jochem Maas wrote: ?php $initial = (is_string($forename) strlen($forename) 0) ? $forename[0] : '' ; echo trim($initial.' '.$surname); ? That sure is verbose Jochem... ?php echo trim( substr( (string)$forename, 0, 1 ).' '.$surname ); ? Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] First Character In A String
Robert Cummings wrote: On Sun, 2007-01-21 at 16:02 +0100, Jochem Maas wrote: ?php $initial = (is_string($forename) strlen($forename) 0) ? $forename[0] : '' ; echo trim($initial.' '.$surname); ? That sure is verbose Jochem... agreed, it was done on purpose in the spirit of 'give the OP a hint about not assuming anything about the input', I could have done this: ?= trim(@$forename[0].' '),$surname; ? which is as short as I can make it :-) it also assumes that the OP actually santized the incoming data ($forename) before doing *anything* with it. ?php echo trim( substr( (string)$forename, 0, 1 ).' '.$surname ); ? Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] First Character In A String
On Sun, 2007-01-21 at 16:27 +0100, Jochem Maas wrote: Robert Cummings wrote: On Sun, 2007-01-21 at 16:02 +0100, Jochem Maas wrote: ?php $initial = (is_string($forename) strlen($forename) 0) ? $forename[0] : '' ; echo trim($initial.' '.$surname); ? That sure is verbose Jochem... agreed, it was done on purpose in the spirit of 'give the OP a hint about not assuming anything about the input', I could have done this: ?= trim(@$forename[0].' '),$surname; ? which is as short as I can make it :-) it also assumes that the OP actually santized the incoming data ($forename) before doing *anything* with it. Oh, I didn't make mine as short as I could, I sanitized it, and didn't cheat by using the error suppression operator ;) echo trim( substr( (string)$forename, 0, 1 ).' '.$surname ); Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] First Character In A String
Robert Cummings wrote: On Sun, 2007-01-21 at 16:27 +0100, Jochem Maas wrote: Robert Cummings wrote: On Sun, 2007-01-21 at 16:02 +0100, Jochem Maas wrote: ?php $initial = (is_string($forename) strlen($forename) 0) ? $forename[0] : '' ; echo trim($initial.' '.$surname); ? That sure is verbose Jochem... agreed, it was done on purpose in the spirit of 'give the OP a hint about not assuming anything about the input', I could have done this: ?= trim(@$forename[0].' '),$surname; ? which is as short as I can make it :-) it also assumes that the OP actually santized the incoming data ($forename) before doing *anything* with it. Oh, I didn't make mine as short as I could, I sanitized it, and didn't cheat by using the error suppression operator ;) consider it my evil streak :-) indeed yours is the better. echo trim( substr( (string)$forename, 0, 1 ).' '.$surname ); now we can get on with having a flame war as to the 'best' way to style your code, because obviously this is better ;-) ... echo trim(substr((string)$forename, 0, 1).' '.$surname); Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] First Character In A String
On Sun, 2007-01-21 at 16:49 +0100, Jochem Maas wrote: now we can get on with having a flame war as to the 'best' way to style your code, because obviously this is better ;-) ... Game on! :B echo trim(substr((string)$forename, 0, 1).' '.$surname); Surely any fool can see that yours is inferior to the following: echo trim( substr( (string)$forename, 0, 1 ).' '.$surname ); Why? (some misguided readers might ask) Because, by placing spaces between function paramater parenthesis, it is clearly easy to distinguish function parameter bounds from expression evaluation precedence parenthesis (say that 5 times fast). :D Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php