[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Before first space / after last space

I want to find everything before first space:

     "Hello There World"

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


0
hankknight
Asked:
hankknight
  • 3
1 Solution
 
youssefomarCommented:
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
 
youssefomarCommented:
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
 
youssefomarCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now