Virajsinh

PhP String to get 5 Words

Jun 22nd, 2021
797
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // http://www.geekysolution.com/how-to-select-first-5-words-from-a-string-in-php/
  4.  
  5. $my_string = 'geekysolution is an awesome mentor in the whole internet';
  6.  
  7. // Break the string into words
  8. $words = explode(' ', $my_string);
  9.  
  10. // Select first five (5) words
  11. $five_words = array_slice($words,0,5);
  12.  
  13. // Combine the five (5) words into string
  14. $String_of_five_words = implode(' ',$five_words)."\n";
  15.  
  16. // Show the string of five words
  17. echo $String_of_five_words;
  18.  
  19. ?>
RAW Paste Data