2017-04-25 130 views
0

我正在使用MySQL全文搜索,并且需要按照下面描述的方式格式化搜索结果。用于格式化搜索结果的PHP字符串函数

是在PHP还是一些库函数,获取指定参数并返回指定结果?
如果不是,有没有人执行过类似的功能?

$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' 

最新通报
我需要得到与此类似。
http://joxi.net/BA01zg3tJ4l5Or

现在我没有desired_function。但我需要它。我需要一个获取描述参数并返回描述结果的函数。

+0

我找到了正确的答案: http://stackoverflow.com/a/23330035/1638298 – FlamyTwista

回答

0

不太清楚你在wordsGapdelimiter,但下面的功能是什么意思将返回数组与找到的词:

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; 
} 

例:

$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); 

输出:

Array 
(
    [0] => word1 
    [1] => word2 
) 

您可以在该行

$mFinalArray[] = $currentNeedle;

如果你想添加的分隔符,wordsGap

玩,但是从时间你有你的数据,你可以显示任何你喜欢的。

+0

嗨。我已经更新了我的问题,现在任务更加清晰。 – FlamyTwista

+0

这对我来说还不够清楚..可以用一个真实的例子来更新你的问题。真实的投入,实际的预期产出是你想要的方式。另外现在你有一个函数,它返回你所需要的.. –