-1

I have this string i want to extract string which is between first occurence of whitespace and last occurence of white space

$string="...ence it is apparent why many are in the favour of the above stance. Another argument i...";

i want to extract this part it is apparent why many are in the favour of the above stance. Another argument

Thanks

user3653474
  • 3,393
  • 6
  • 49
  • 135

2 Answers2

1
$firstIndex = strpos($string, ' ') + 1;
$lastIndex = strrpos($string, ' ');
substring($string, $firstIndex, $lastIndex - $firstIndex);

Reference: indexOf and lastIndexOf in PHP?

Tony Yip
  • 705
  • 5
  • 14
1

There are many ways to do this, here is one:

<?php
$str_arr = explode(" ", "Your long sentence here.");
$sub_arr = array_slice($str_arr, 1, count($str_arr)-2);
$extracted_str = implode(' ', $sub_arr);
echo $extracted_str; // long sentence
Pezhvak
  • 9,633
  • 7
  • 29
  • 39