bp_search_result_match( $in,  $wordToFind, int $numWordsToWrap = 10 )

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

Description

Parameters

$in

(Required)

$wordToFind

(Required)

$numWordsToWrap

(int) (Optional)

Default value: 10

Return

(string)

Source

File: bp-search/bp-search-functions.php

function bp_search_result_match( $in, $wordToFind, $numWordsToWrap = 10 ) {

	$words       = preg_split( '/\s+/', $in );
	$wordsToFind = preg_split( '/\s+/', $wordToFind );

	foreach ( $wordsToFind as $key => $value ) {
		$found_words = preg_grep( "/" . $value . ".*/i", $words );
		$found_pos   = array_keys( $found_words );

		if ( count( $found_pos ) ) {
			$pos = $found_pos[0];
			break;
		}
	}

	if ( isset( $pos ) ) {

		$start  = ( $pos - $numWordsToWrap > 0 ) ? $pos - $numWordsToWrap : 0;
		$length = ( ( $pos + ( $numWordsToWrap + 1 ) < count( $words ) ) ? $pos + ( $numWordsToWrap + 1 ) : count( $words ) ) - $start;
		$slice  = array_slice( $words, $start, $length );

		$pre_start = ( $start > 0 ) ? "&hellip;" : "";

		$post_end = ( $pos + ( $numWordsToWrap + 1 ) < count( $words ) ) ? "&hellip;" : "";

		$out = $pre_start . implode( ' ', $slice ) . $post_end;

		return $out;
	}

	return $in;

}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.