0

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.

1

2 Answers 2

0

Looks like you need to use the php function http://php.net/manual/en/function.strpos.php

Sign up to request clarification or add additional context in comments.

1 Comment

Hi. I've updated my question, so task is more clear now.
0

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.

2 Comments

Hi. I've updated my question, so task is more clear now.
That's not clear enough for me..Maybe update your question with a real example. Real inputs, real expected outputs the way you want them. Plus right now you have a function which returns what you need..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.