Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Get first word in string

Posted on 2005-04-05
Medium Priority
Last Modified: 2010-07-27
I've got a string like this: "John Smith", and I want to just get the first name, what's the easiest way to do this? In fact, is there a function that will split out all the words?
Question by:andycal
  • 2
LVL 49

Assisted Solution

Roonaan earned 300 total points
ID: 13705687
Try this:

  $name = "John Smith";
  list($firstname, $everythingelse) = explode(" ", trim($name), 2);

LVL 14

Accepted Solution

Esopo earned 300 total points
ID: 13705692
Addign to Roonaan:
Use the explode function (as in Roonaan's example)


$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

Author Comment

ID: 13705718
Thanks guys!
LVL 14

Expert Comment

ID: 13705830
You're welcome.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

571 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