Php first letter of word

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:

  1. The string in question.
  2. The index to start at (we set this to 0 because we want to start at the very beginning of the string).
  3. The length that we want to return (we set this to 1 because we only want the first character).
Читайте также:  Python визуализация данных excel

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

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

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

s.fox is offlineAwesome!

QuestionPHP: 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.

ghostdog74 is offlineI 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.

hastur is offline5 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

lapubell is offlineGee! 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.

s.fox is offlineAwesome!

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.

jimi_hendrix is offlineSkinny Extra Sweet Ubuntu

Re: PHP: Find 1st Letter of EVERY word in a string

QuoteOriginally Posted by lapubell View Post

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.

s.fox is offlineAwesome!

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

myrtle1908 is offlineDipped 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;

Can+~ is offlineUbuntu 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

s.fox is offlineAwesome!

Re: PHP: Find 1st Letter of EVERY word in a string

QuoteOriginally Posted by Can+~ View Post

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
    1. Ubuntu Official Flavours Support
      1. New to Ubuntu
      2. General Help
      3. Installation & Upgrades
      4. Hardware
      5. Desktop Environments
      6. Networking & Wireless
      7. Multimedia Software
    2. Ubuntu Specialised Support
      1. Ubuntu Development Version
      2. Security
      3. Virtualisation
      4. Ubuntu Servers, Cloud and Juju
        1. Server Platforms
        2. Ubuntu Cloud and Juju
      5. Gaming & Leisure
        1. Emulators
      6. Wine
      7. Development & Programming
        1. Packaging and Compiling Programs
        2. Development CD/DVD Image Testing
        3. Ubuntu Application Development
        4. Ubuntu Dev Link Forum
        5. Programming Talk
        6. Repositories & Backports
          1. Ubuntu Backports
            1. Bug Reports / Support
      8. System76 Support
      9. Apple Hardware Users
    3. Ubuntu Community Discussions
      1. Ubuntu, Linux and OS Chat
        1. Recurring Discussions
        2. Full Circle Magazine
      2. The Cafe
        1. Cafe Games
      3. Market
      4. Mobile Technology Discussions (CLOSED)
      5. Announcements & News
      6. Weekly Newsletter
      7. Membership Applications
      8. The Fridge Discussions
      9. Forum Council Agenda
      10. Forum Feedback & Help
        1. Request a LoCo forum
      11. Resolution Centre
    4. Other Discussion and Support
      1. Other OS Support and Projects
        1. Other Operating Systems
          1. Ubuntu/Debian BASED
          2. Debian
          3. MINT
          4. Arch and derivatives
          5. Fedora/RedHat and derivatives
          6. Mandriva/Mageia
          7. Slackware and derivatives
          8. openSUSE and SUSE Linux Enterprise
          9. Mac OSX
          10. PCLinuxOS
          11. Gentoo and derivatives
          12. Windows
          13. BSD
          14. Any Other OS
      2. Assistive Technology & Accessibility
      3. Art & Design
      4. Education & Science
      5. Documentation and Community Wiki Discussions
      6. Tutorials
        1. Outdated Tutorials & Tips
      7. Ubuntu Women
      8. Ubuntu LoCo Team Forums
        1. Americas LoCo Teams
          1. Argentina Team
            1. Software
            2. Hardware
            3. Comunidad
          2. Arizona Team — US
          3. Arkansas Team — US
          4. Brazil Team
          5. California Team — US
          6. Canada Team
          7. Centroamerica Team
          8. Chile Team
            1. Comunidad
            2. Hardware
            3. Software
            4. Instalaci�n y Actualizaci�n
          9. Colombia Team — Colombia
          10. Georgia Team — US
          11. Illinois Team
          12. Indiana — US
          13. Kentucky Team — US
          14. Maine Team — US
          15. Minnesota Team — US
          16. Mississippi Team — US
          17. Nebraska Team — US
          18. New Mexico Team — US
          19. New York — US
          20. North Carolina Team — US
          21. Ohio Team — US
          22. Oklahoma Team — US
          23. Oregon Team — US
          24. Pennsylvania Team — US
          25. Peru Team
          26. Texas Team — US
          27. Uruguay Team
          28. Utah Team — US
          29. Virginia Team — US
          30. West Virginia Team — US
        2. Asia and Oceania LoCo Teams
          1. Australia Team
          2. Bangladesh Team
          3. Hong Kong Team
          4. Myanmar Team
          5. Philippine Team
          6. Singapore Team
        3. Europe, Middle East, and African (EMEA) LoCo Teams
          1. Albania Team
          2. Catalan Team
          3. Portugal Team
          4. Egypt Team
          5. Georgia Team
          6. Ireland Team — Ireland
          7. Kenyan Team — Kenya
          8. Kurdish Team — Kurdistan
          9. Lebanon Team
          10. Morocco Team
          11. Saudi Arabia Team
          12. Sudan Team
          13. Tunisia Team
        4. Other Forums & Teams
        5. LoCo Archive
          1. Afghanistan Team
          2. Alabama Team — US
          3. Alaska Team — US
          4. Algerian Team
          5. Andhra Pradesh Team — India
          6. Austria Team
          7. Bangalore Team
          8. Bolivia Team
          9. Cameroon Team
          10. Colorado Team — US
          11. Connecticut Team
          12. Costa Rica Team
          13. Delhi Team
          14. Ecuador Team
          15. El Salvador Team
          16. Florida Team — US
          17. Galician LoCo Team
          18. Greek team
          19. Hawaii Team — US
          20. Honduras Team
          21. Idaho Team — US
          22. Iowa Team — US
          23. Jordan Team
          24. Kansas Team — US
          25. Libya Team
          26. Louisiana Team — US
          27. Maryland Team — US
          28. Massachusetts Team
          29. Michigan Team — US
          30. Missouri Team — US
          31. Montana Team — US
          32. Namibia Team
          33. Nevada Team — US
          34. New Hampshire Team — US
          35. New Jersey Team — US
          36. Northeastern Team — US
          37. Panama Team
          38. Paraguay Team
          39. Qatar Team
          40. Quebec Team
          41. Rhode Island Team — US
          42. Senegal Team
          43. South Carolina Team — US
          44. South Dakota Team — US
          45. Switzerland Team
          46. Tamil Team — India
          47. Tennessee Team — US
          48. Trinidad & Tobago Team
          49. Uganda Team
          50. United Kingdom Team
          51. US LoCo Teams
          52. Venezuela Team
          53. Wales Team
          54. Washington DC Team — US
          55. Washington State Team — US
          56. Wisconsin Team
          57. Yemen Team
          58. Za Team — South Africa
          59. Zimbabwe Team

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

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

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.

Источник

Оцените статью