I'm using MySQL fulltext search and need to format search result in described below manner.
Is in PHP or some library function, that gets specified parameters and returns specified result?
If no, has anyone an implementation of similar function?
$needles = ['word1', 'word2'];
$haystack = 'This string contains word1. Also this string contains word2';
$wordsGap = 1; //word quantity from left and right of every in $result;
$delimiter = '...';
$result = desired_function($needles, $haystack, $wordsGap, $delimiter);
//$result must be String like '... contains word1. Also ... contains word2'
UPDATES
I need result similar to this one.
http://joxi.net/BA01zg3tJ4l5Or
For now I HAVEN'T desired_function. But I need it. I need a function that gets described parameters and returns described result.
Looks like you need to use the php function http://php.net/manual/en/function.strpos.php
Not quite sure what you meant by the wordsGap
and delimiter
but the below functions will return an array with the found words :
function cool_function($needles, $haystack, $wordGap, $delimiter){
$haystack = explode(' ', $haystack);
$mFinalArray = [];
foreach($haystack as $current){
foreach($needles as $currentNeedle){
if(strpos($current, $currentNeedle) !== false){
$mFinalArray[] = $currentNeedle;
}
}
}
return $mFinalArray;
}
Example :
$needles = ['word1', 'word2'];
$haystack = 'This string contains word1. Also this string contains word2';
$wordsGap = 1; //word quantity from left and right of every in $result;
$delimiter = '...';
$result = cool_function($needles, $haystack, $wordsGap, $delimiter);
Output :
Array
(
[0] => word1
[1] => word2
)
You can play at the line
$mFinalArray[] = $currentNeedle;
if you want to add the delimiter and wordsGap, but from the time you have your data you can show whatever you like.