envexlabs Posted February 12, 2008 Share Posted February 12, 2008 I have a search page that searches through a database of posts. Lets say i searched for "passage". I've used str_replace to bold and highlight the searched term, but i also only want to display 15 words before and after the searched term. Right now i have: Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. I want: ...Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source... Does anyone know how to go about doing this? envex Quote Link to comment https://forums.phpfreaks.com/topic/90761-display-15-words-before-and-after-a-set-word/ Share on other sites More sharing options...
Barand Posted February 12, 2008 Share Posted February 12, 2008 try <?php $text = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. '; $len = strlen($text); $posx = strpos($text, 'passage'); $pos = $posx; $count = 0; while ($count < 16) { $c = $text[--$pos]; if ($c==' ') $count++; if ($pos <= 0) { $pos = 0; break; } } $pos1 = $posx; $count = 0; while ($count < 16) { $c = $text[++$pos1]; if ($c==' ') $count++; if ($pos1 >= $len) { $pos1 = $len; break; } } $text = substr($text, $pos, $pos1-$pos); echo str_replace('passage', "<span style='color: #F00; font-weight: 600'>passage</span>", $text); ?> Quote Link to comment https://forums.phpfreaks.com/topic/90761-display-15-words-before-and-after-a-set-word/#findComment-465266 Share on other sites More sharing options...
envexlabs Posted February 12, 2008 Author Share Posted February 12, 2008 Worked like a charm! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/90761-display-15-words-before-and-after-a-set-word/#findComment-465288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.