Jump to content

display 15 words before and after a set word.


envexlabs

Recommended Posts

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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.