?
Solved

Get first word in string

Posted on 2005-04-05
4
Medium Priority
?
439 Views
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?
0
Comment
Question by:andycal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 300 total points
ID: 13705687
Try this:

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

-r-
0
 
LVL 14

Accepted Solution

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

http://us3.php.net/explode

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

Author Comment

by:andycal
ID: 13705718
Thanks guys!
0
 
LVL 14

Expert Comment

by:Esopo
ID: 13705830
You're welcome.
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

762 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