Solved

Before first space / after last space

Posted on 2009-03-29
3
200 Views
Last Modified: 2012-05-06
I want to find everything before first space:

     "Hello There World"

I want $first to be "Hello"
I want $last to be "World"


0
Comment
Question by:hankknight
[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
  • 3
3 Comments
 
LVL 6

Expert Comment

by:youssefomar
ID: 24014046
one quick solution is to convert to array and pint first and last



<?
  arr = explode(" ", "Hello There World");
 
  echo "First: ". arr[0];
  echo "Last: " . arr[count(arr)-1];
  
 
?>

Open in new window

0
 
LVL 6

Accepted Solution

by:
youssefomar earned 500 total points
ID: 24014085
or you use strpos and strripos to find 1st and last spaces:

<?
	$str =  "Hello There World";
	$first_space = strpos($str, " ");
	$last_space = strripos($str, " ");
	
	echo "first: " . substr($str,0,$first_space);
	echo "<br>last: " . substr($str,$last_space);
 
?>

Open in new window

0
 
LVL 6

Expert Comment

by:youssefomar
ID: 24014094
sorry I didn't type the $ before the arr variable in the 1st example, here it is again

<?
 
  $arr = explode(" ", "Hello There World");
 
  echo "First: ". $arr[0];
  echo "<br>Last: " . $arr[count($arr)-1];
?>

Open in new window

0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

696 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