Home > OS >  Extract string between first whitespace and last whitespace in php
Extract string between first whitespace and last whitespace in php

Time:05-14

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

CodePudding user response:

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

CodePudding user response:

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

Reference: indexOf and lastIndexOf in PHP?

  •  Tags:  
  • php
  • Related