Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Result displaying-like google php

Tags:

php

i have an paragraph and user will search inside that and if the search term has 3 matches inside but all are in 3 different places

ex

World War II, or the Second World War[1] (often abbreviated WWII or WW2), was a global military conflict lasting from 1939 to 1945 which involved most of the world's nations, including all of the great powers, organised into two opposing military alliances: the Allies and the Axis. It was the most widespread war in history, with more than 100 million military personnel mobilised. In a state of "total war," the major participants placed their entire economic, industrial, and scientific capabilities at the service of the war effort, erasing the distinction between civilian and military resources. Marked by significant action against civilians, including the Holocaust and the only use of nuclear weapons in warfare, it was the deadliest conflict in human history,[2] with over seventy million casualties.

i have to search "war" so that it should display like

World War II, or the Second World War[1].....In a state of "totalwar,"....

like image 877
Ramesh Avatar asked Jan 27 '26 15:01

Ramesh


1 Answers

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

You can read more about preg_replace and regular expressions in the manual.

like image 174
Emil Vikström Avatar answered Jan 29 '26 03:01

Emil Vikström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!