[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Before first space / after last space

Posted on 2009-03-29
3
Medium Priority
?
202 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 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

656 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