- PHP: Get the first character in a string.
- mb_substr and special characters.
- PHP ucfirst() Function
- Syntax
- Parameter Values
- Technical Details
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- Thread: PHP: Find 1st Letter of EVERY word in a string
- PHP: Find 1st Letter of EVERY word in a string
- Re: PHP: Find 1st Letter of EVERY word in a string
- Re: PHP: Find 1st Letter of EVERY word in a string
- Re: PHP: Find 1st Letter of EVERY word in a string
- Re: PHP: Find 1st Letter of EVERY word in a string
- Re: PHP: Find 1st Letter of EVERY word in a string
- Re: PHP: Find 1st Letter of EVERY word in a string
- Re: PHP: Find 1st Letter of EVERY word in a string
- Re: PHP: Find 1st Letter of EVERY word in a string
- Re: PHP: Find 1st Letter of EVERY word in a string
- Bookmarks
- Posting Permissions
- PHP ucfirst() Function
- Syntax
- Parameter Values
- Technical Details
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
PHP: Get the first character in a string.
This is a short guide on how to get the first character of a string in PHP. As an added bonus, I will also show you how to get the first two characters of a string.
We used two different approaches here.
Using the first approach, we treat the string like an array. i.e. We use the index position of the character to access it. This is possible because strings in PHP can be treated like character arrays (basically, a string is just an array that consists of characters).
In the second approach, we used the PHP function substr, which returns a part of a string. With the substr function, there are three parameters:
- The string in question.
- The index to start at (we set this to 0 because we want to start at the very beginning of the string).
- The length that we want to return (we set this to 1 because we only want the first character).
mb_substr and special characters.
If there is a chance that your string contains special characters such as “ö” or “ë”, then you will need to use the mb_substr function. This function is the multi-byte safe version of substr. If you use the substr on a string that contains special characters, then you might end up with jumbled characters and / or the black diamond question mark “replacement character”.
Example of mb_substr being used to get the first character of a string that contains special characters:
//Set the header to utf-8 for example purposes. header('Content-Type: text/html; charset=utf-8'); //Example string. $string = 'öëBC'; //Use mb_substr to get the first character. $firstChar = mb_substr($string, 0, 1, "UTF-8"); //Print out the first character. echo $firstChar;
Note that this function allows you to specify the encoding as a fourth parameter!
Anyway; hopefully you found this short tutorial helpful!
PHP ucfirst() Function
The ucfirst() function converts the first character of a string to uppercase.
- lcfirst() — converts the first character of a string to lowercase
- ucwords() — converts the first character of each word in a string to uppercase
- strtoupper() — converts a string to uppercase
- strtolower() — converts a string to lowercase
Syntax
Parameter Values
Technical Details
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Thread: PHP: Find 1st Letter of EVERY word in a string
Awesome!
PHP: Find 1st Letter of EVERY word in a string
How would I find the first letter of a word contained within a string using PHP.
$string = "Lots of words in this string";
I would want to find L,O,W,I,T,S. Any thoughts? I know how to find the very first letter, but this is slightly more complicated.
I Ubuntu, Therefore, I Am
Re: PHP: Find 1st Letter of EVERY word in a string
split the string up into an array, then use for loop to go through the array, checking the first letter using substring functions.
5 Cups of Ubuntu
Re: PHP: Find 1st Letter of EVERY word in a string
stupid idea : with a regular expression and a capturing pattern.
something like
preg_match_all("/(\S)\S*/i",$string,$array,PREG_PATTERN_ORDER)
then $array[0] or $array[1] should contain all the first letters. hopefully (my regexp may be totally wrong).
but of course exploding the string is simpler
Gee! These Aren’t Roasted!
Re: PHP: Find 1st Letter of EVERY word in a string
real quick code. debug on your own. probably not the fastest solution by far
$string = «Lots of words in this string»; $words = explode(» «, $string); $letters = «»; foreach ($words as $value)
if you want the spaces and commas in there, then put those in the for loop too.
Awesome!
Re: PHP: Find 1st Letter of EVERY word in a string
Thanks for the ideas. I have managed to accomplish my goal by splitting the string into an array. I just wondered if their was an alternative.
Skinny Extra Sweet Ubuntu
Re: PHP: Find 1st Letter of EVERY word in a string
Originally Posted by lapubell
real quick code. debug on your own. probably not the fastest solution by far
$string = «Lots of words in this string»; $words = explode(» «, $string); $letters = «»; foreach ($words as $value)
if you want the spaces and commas in there, then put those in the for loop too.
Awesome!
Re: PHP: Find 1st Letter of EVERY word in a string
Not just saying this but explode has got to be in my top 3 most used commands in PHP.
I was only reluctant to use it in this case because I wanted to see if their were any alternatives and wanted to increase my PHP knowledge.
Again, Thanks to the community
Dipped in Ubuntu
Re: PHP: Find 1st Letter of EVERY word in a string
my $s = "one two three four"; my @f = ($s =~ m/\b(\w)/g); print @f;
Ubuntu Cappuccino Scuro
Re: PHP: Find 1st Letter of EVERY word in a string
«Just in terms of allocation of time resources, religion is not very efficient. There’s a lot more I could be doing on a Sunday morning.»
-Bill Gates
Awesome!
Re: PHP: Find 1st Letter of EVERY word in a string
Originally Posted by Can+~
Now that is an idea i had not considered. I know I have already done what I set out to do but that kinda sounds like a fun problem to solve.
- Site Areas
- Settings
- Private Messages
- Subscriptions
- Who’s Online
- Search Forums
- Forums Home
- Forums
- The Ubuntu Forum Community
- Ubuntu Official Flavours Support
- New to Ubuntu
- General Help
- Installation & Upgrades
- Hardware
- Desktop Environments
- Networking & Wireless
- Multimedia Software
- Ubuntu Specialised Support
- Ubuntu Development Version
- Security
- Virtualisation
- Ubuntu Servers, Cloud and Juju
- Server Platforms
- Ubuntu Cloud and Juju
- Gaming & Leisure
- Emulators
- Wine
- Development & Programming
- Packaging and Compiling Programs
- Development CD/DVD Image Testing
- Ubuntu Application Development
- Ubuntu Dev Link Forum
- Programming Talk
- Repositories & Backports
- Ubuntu Backports
- Bug Reports / Support
- Ubuntu Backports
- System76 Support
- Apple Hardware Users
- Ubuntu Community Discussions
- Ubuntu, Linux and OS Chat
- Recurring Discussions
- Full Circle Magazine
- The Cafe
- Cafe Games
- Market
- Mobile Technology Discussions (CLOSED)
- Announcements & News
- Weekly Newsletter
- Membership Applications
- The Fridge Discussions
- Forum Council Agenda
- Forum Feedback & Help
- Request a LoCo forum
- Resolution Centre
- Ubuntu, Linux and OS Chat
- Other Discussion and Support
- Other OS Support and Projects
- Other Operating Systems
- Ubuntu/Debian BASED
- Debian
- MINT
- Arch and derivatives
- Fedora/RedHat and derivatives
- Mandriva/Mageia
- Slackware and derivatives
- openSUSE and SUSE Linux Enterprise
- Mac OSX
- PCLinuxOS
- Gentoo and derivatives
- Windows
- BSD
- Any Other OS
- Other Operating Systems
- Assistive Technology & Accessibility
- Art & Design
- Education & Science
- Documentation and Community Wiki Discussions
- Tutorials
- Outdated Tutorials & Tips
- Ubuntu Women
- Ubuntu LoCo Team Forums
- Americas LoCo Teams
- Argentina Team
- Software
- Hardware
- Comunidad
- Arizona Team — US
- Arkansas Team — US
- Brazil Team
- California Team — US
- Canada Team
- Centroamerica Team
- Chile Team
- Comunidad
- Hardware
- Software
- Instalaci�n y Actualizaci�n
- Colombia Team — Colombia
- Georgia Team — US
- Illinois Team
- Indiana — US
- Kentucky Team — US
- Maine Team — US
- Minnesota Team — US
- Mississippi Team — US
- Nebraska Team — US
- New Mexico Team — US
- New York — US
- North Carolina Team — US
- Ohio Team — US
- Oklahoma Team — US
- Oregon Team — US
- Pennsylvania Team — US
- Peru Team
- Texas Team — US
- Uruguay Team
- Utah Team — US
- Virginia Team — US
- West Virginia Team — US
- Argentina Team
- Asia and Oceania LoCo Teams
- Australia Team
- Bangladesh Team
- Hong Kong Team
- Myanmar Team
- Philippine Team
- Singapore Team
- Europe, Middle East, and African (EMEA) LoCo Teams
- Albania Team
- Catalan Team
- Portugal Team
- Egypt Team
- Georgia Team
- Ireland Team — Ireland
- Kenyan Team — Kenya
- Kurdish Team — Kurdistan
- Lebanon Team
- Morocco Team
- Saudi Arabia Team
- Sudan Team
- Tunisia Team
- Other Forums & Teams
- LoCo Archive
- Afghanistan Team
- Alabama Team — US
- Alaska Team — US
- Algerian Team
- Andhra Pradesh Team — India
- Austria Team
- Bangalore Team
- Bolivia Team
- Cameroon Team
- Colorado Team — US
- Connecticut Team
- Costa Rica Team
- Delhi Team
- Ecuador Team
- El Salvador Team
- Florida Team — US
- Galician LoCo Team
- Greek team
- Hawaii Team — US
- Honduras Team
- Idaho Team — US
- Iowa Team — US
- Jordan Team
- Kansas Team — US
- Libya Team
- Louisiana Team — US
- Maryland Team — US
- Massachusetts Team
- Michigan Team — US
- Missouri Team — US
- Montana Team — US
- Namibia Team
- Nevada Team — US
- New Hampshire Team — US
- New Jersey Team — US
- Northeastern Team — US
- Panama Team
- Paraguay Team
- Qatar Team
- Quebec Team
- Rhode Island Team — US
- Senegal Team
- South Carolina Team — US
- South Dakota Team — US
- Switzerland Team
- Tamil Team — India
- Tennessee Team — US
- Trinidad & Tobago Team
- Uganda Team
- United Kingdom Team
- US LoCo Teams
- Venezuela Team
- Wales Team
- Washington DC Team — US
- Washington State Team — US
- Wisconsin Team
- Yemen Team
- Za Team — South Africa
- Zimbabwe Team
- Americas LoCo Teams
- Other OS Support and Projects
- Ubuntu Official Flavours Support
Bookmarks
Bookmarks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
PHP ucfirst() Function
The ucfirst() function converts the first character of a string to uppercase.
- lcfirst() — converts the first character of a string to lowercase
- ucwords() — converts the first character of each word in a string to uppercase
- strtoupper() — converts a string to uppercase
- strtolower() — converts a string to lowercase
Syntax
Parameter Values
Technical Details
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.