buddyboss_global_search_result_match

Find a certain word in a string, and then wrap around it.

Description

(string) buddyboss_global_search_result_match( $in, $wordToFind, (int) $numWordsToWrap = 10 ); 

Returns (string)

Parameters (3)

0. $in
The in.
1. $wordtofind
The wordtofind.
2. $numwordstowrap (int)
The numwordstowrap.

Usage

  1. if ( !function_exists( 'buddyboss_global_search_result_match' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress-global-search/includes/functions.php'; 
  3.  
  4. // The in. 
  5. $in = null; 
  6.  
  7. // The wordtofind. 
  8. $wordtofind = null; 
  9.  
  10. // The numwordstowrap. 
  11. $numwordstowrap = -1; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = buddyboss_global_search_result_match($in, $wordtofind, $numwordstowrap); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/functions.php  
  1. function buddyboss_global_search_result_match( $in, $wordToFind, $numWordsToWrap = 10 ) { 
  2.  
  3. $words = preg_split('/\s+/', $in); 
  4. $wordsToFind = preg_split('/\s+/', $wordToFind); 
  5.  
  6. foreach ( $wordsToFind as $key => $value ) { 
  7. $found_words = preg_grep("/".$value.".*/i", $words); 
  8. $found_pos = array_keys($found_words); 
  9.  
  10. if( count( $found_pos ) ) { 
  11. $pos = $found_pos[0]; 
  12. break; 
  13.  
  14. if (isset($pos)) { 
  15.  
  16. $start = ($pos - $numWordsToWrap > 0) ? $pos - $numWordsToWrap : 0; 
  17. $length = (($pos + ($numWordsToWrap + 1) < count($words)) ? $pos + ($numWordsToWrap + 1) : count($words)) - $start; 
  18. $slice = array_slice($words, $start, $length); 
  19.  
  20. $pre_start = ($start > 0) ? "...":""; 
  21.  
  22. $post_end = ($pos + ($numWordsToWrap + 1) < count($words)) ? "...":""; 
  23.  
  24. $out = $pre_start.implode(' ', $slice).$post_end; 
  25. return $out; 
  26.  
  27. return $in; 
  28.