Solved

Before first space / after last space

Posted on 2009-03-29
3
192 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
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 …

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now