Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于格式化搜索结果的PHP字符串函数_Php_String_Search_Formatting - Fatal编程技术网

用于格式化搜索结果的PHP字符串函数

用于格式化搜索结果的PHP字符串函数,php,string,search,formatting,Php,String,Search,Formatting,我使用的是MySQL全文搜索,需要按照下面描述的方式格式化搜索结果 在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

我使用的是MySQL全文搜索,需要按照下面描述的方式格式化搜索结果

在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'
更新
我需要类似于此的结果。


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

看起来您需要使用php函数不太确定
单词gap
分隔符
是什么意思,但是下面的函数将返回一个包含找到的单词的数组:

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[]=$currentPionel


如果您想添加分隔符和单词gap,但从您有数据时起,您可以显示任何您喜欢的内容。

Hi。我已经更新了我的问题,所以现在任务更清楚了。这对我来说还不够清楚。也许可以用一个真实的例子更新你的问题。真实的输入,真实的预期输出,你想要的方式。另外,现在你有一个函数,它返回你需要的东西。我已经更新了我的问题,所以现在任务更清楚了。我在那里找到了正确的答案: