Search result displayed as google php

I have a paragraph and the user will search inside it, and if the search query has 3 matches inside, but they are all in three different places.

ex

World War II or World War II (often abbreviated World War II or World War II) was a global military conflict that lasted from 1939 to 1945, in which the majority of the states of the world participated, including all the great powers organized in two warring military alliances: allies and axis. It was the most widespread war in history, in which more than 100 million troops were mobilized. In a state of "total war", the main participants put all their economic, industrial and scientific potential at the service of military efforts, blurring the distinction between civilian and military resources. Noted by significant actions against civilians, including the Holocaust and the only use of nuclear weapons in war,it was the deadliest conflict in human history [2] with more than seventy million victims.

I need to look for a “war” so that it displays as

World War II, or World War II [1] ..... In the state of "total war ," ....

+3
source share
2 answers
$text = 'World War II, or the Second World W...';
$text = preg_replace('/war/ig', '<strong>$0</strong>', $text);

Learn more about preg_replace and regular expressions in the manual.

+1
source

I do not have a PHP solution, but I suggest you do this on the client side. This means that you can cache any output server side and save several loops :)

Disclaimer . This is my own project.

searchTermsHighlight, jQuery. . , .

, PHP, stri_replace(), , . , , .

$searchTerms = array('war');

foreach($searchTerms as $term) {

    $content = stri_replace($term, '<span class="highlight">' . $term . '</strong>', $content);

}

stri_replace(), .

0

Source: https://habr.com/ru/post/1746509/


All Articles