Solved

Before first space / after last space

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 60
PHP-Client-Registration-Login article and  saving last last login 6 29
write screen output to text file 21 47
mysql date time 14 30
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…

772 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