'Extract string between first whitespace and last whitespace in php
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
Solution 1:[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
Solution 2:[2]
$firstIndex = strpos($string, ' ') + 1;
$lastIndex = strrpos($string, ' ');
substring($string, $firstIndex, $lastIndex - $firstIndex);
Reference: indexOf and lastIndexOf in PHP?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pezhvak |
Solution 2 | Tony Yip |