Home > OS >  How to replace matching characters in two sets of strings with bolded characters?
How to replace matching characters in two sets of strings with bolded characters?

Time:11-18

currently working on a following function:

public function boldText($searchSuggestions)
{
    $search = $this->getRequestParameter('search');
    $pattern = "/".$search."/u";
    $searchSuggestions = preg_replace($pattern, '<b>'.$search.'</b>', $searchSuggestions);
    echo $searchSuggestions;
}

Let's say $searchSuggestions = hello While the user is typing in the search box, which in this case the variable $search contains this input, a dropdown menu of all possible result suggestions are displayed. If a user types 'hello', then the search results like 'helloworld' or 'hello2' would pop up and the inputted word, int this case 'hello' would be bold in all outputted search results. So far it is working fine, however, big Characters are being replaced with small Characters and vice versa in the outputted search results. I have a feeling that the underlying problem might be in this function, however I am not entirely sure. If anyone has any suggestions or tips on where to look, it would be great!

If I should give out more info please do let me know, and I will edit the question immediately.

Thankyou!

Example output currently - User types in search bar - 'hello' result shown should be - 'Hello' result actually being shown - 'hello'

P.S The results are accessed from an sql query. If a user types, than a query that gets data related to the words inputted is shown. For instance - 'SELECT * FROM test WHERE example LIKE '%hello%' In database one can find the word Hello. Note the H has a big character.

I tried this following code

public function boldText($searchSuggestions)
{
    $search = $this->getRequestParameter('search');
    $pattern = "/".$search."/u";
    $searchSuggestions = preg_replace($pattern, '<b>'.$search.'</b>', $searchSuggestions);
    echo $searchSuggestions;
}

Brief Explanation $searchSuggestions

Hello

$search(being typed by the user)

h

The output should be

<b>H</b>ello

The output I am currently getting is

<b>h</b>ello

CodePudding user response:

I tried to look further on how to properly solve this and it was a bit simpler than I expected. Did not have to use preg_replace for this. The following code did the trick for me:

public function boldText($searchSuggestions)
    {
        $search = $this->getRequestParameter('search');
        $lastPosition = strlen($search);
        $firstPosition = stripos($searchSuggestions, $search);
        $replacement = substr($searchSuggestions, $firstPosition, $lastPosition);
        $searchSuggestions = substr_replace($searchSuggestions, '<b><u>'.$replacement.'</u></b>', $firstPosition, $lastPosition);
        echo $searchSuggestions;
    }

$lastPosition - we find how long the search input is, therfore getting the last position

$firstPosition - compare the two strings by finding a match(case insensitive), therefore finding the first position

$replacement - we remove what matches between search and searchSuggestion from the searchSuggestion string, with the help of the firstPosition and lastPosition variables.

At the end the searchSuggestion variable is replaced accordingly. Hope I was clear enough if not please let me know. (Answered my own question lol!)

CodePudding user response:

You need to capture the found text you then can use that in the replacement bit. Your current implementation uses the input which may or may not match the case of the original string. So you should:

  1. use the i modifier so your search is case insensitive

  2. use word boundaries so partial matching doesn't occur

  3. use capture group to capture the match

  4. use preg_quote so any special regex characters don't affect regex operations

    public function boldText($searchSuggestions)
    {
        $search = $this->getRequestParameter('search');
        $pattern = "/\b(". preg_quote($search) . ")\b/iu";
        $searchSuggestions = preg_replace($pattern, '<b>$1</b>', $searchSuggestions);
        echo $searchSuggestions;
    }
    

If partial word matching is intended you can remove word boundaries:

public function boldText($searchSuggestions)
        {
            $search = $this->getRequestParameter('search');
            $pattern = "/(". preg_quote($search) . ")/iu";
            $searchSuggestions = preg_replace($pattern, '<b>$1</b>', $searchSuggestions);
            echo $searchSuggestions;
        }

consider https://en.wikipedia.org/wiki/Scunthorpe_problem#Origin_and_history also though.

  • Related