Jump to content

Find first letter of a sentence


The Little Guy

Recommended Posts

OK... I am in the process of making a function, what this function will do, is take a body of text, and strip it down, and bold all the words in the array of words.

 

A word can be bold in in the center of a sentence, so what I would like to know, is how to find the the position of the first letter of the bold word, and go backwards until I find the first letter in the sentence.

 

This letter will start the text of the displayed content, and will show 50 words, or the first two sentences.

Link to comment
Share on other sites

I'm very confused by your description.

 

In your second sentence, you ask how to find the first letter of a bold word, and then the first letter of that sentence.  How do you know that a letter is bold (or should be bold)?

Link to comment
Share on other sites

A word is bold because it is in an array of words, if it isn't bold, then its not in the array.

 

All I really want to do is find the position of the first word of a sentence, if that sentence contains a bold word.

 

Then I want to get that sentence, and the following sentence, if the two sentences combined is less than 50 words, I want it  to find the first 50 words of the of the first sentence.... (W/E)

 

I just need to find the first word of the sentence of the bold word.

 

Examples:

 

I like to eat lots and lots of pie.

 

"lots" is bold, so I would like to find the first word of that sentence, and start the output there. once it has outputted 2 sentences, then it stop outputting.

Link to comment
Share on other sites

Oh I get it.

 

$bold_words = array(
  'bold',
  'powerpuff',
);
# Find the first word of each sentence containing a word from $bold_words
# Then find the first 50 (or some other fixed number) words from that sentence
# Then display each sentence, with the bold word bolded

 

Is that the right idea?

Link to comment
Share on other sites

Oh I get it.

 

$bold_words = array(
  'bold',
  'powerpuff',
);
# Find the first word of each sentence containing a word from $bold_words
# Then find the first 50 (or some other fixed number) words from that sentence
# Then display each sentence, with the bold word bolded

 

Is that the right idea?

 

yeah... I think.

 

 

I am a super elephant. My monkey likes me. I really don't care what you think.

 

Pretend the above is a normal paragraph. "likes" is the bold word. So... the text that would be displayed on the screen is:

 

My monkey likes me. I really don't care what you think.

 

The very first sentence would be dropped, and the 2 sentences would be displayed, starting with the sentence containing the first occurrence of a bold word from the array.

 

Here is a code someone gave me, it finds the first bold word, and counts back how every many you tell it. the problem with it is that it doesn't care if it cuts a word/sentence in half. I would either like to fix it so it starts at the beginning of the sentence, or make a new one.

<?php
function abbreviate_and_bold($content, $words_array, $length, $backtrack)
{
    # str $content -> text string
    # arr $words_array -> array of words to "bold"
    # int $length -> truncated length of the returned string
    # int $backtrack -> number of characters to display before the first word in the words array
    $content = strip_tags($content);
    if(($strlen = strlen($content)) > $length)
    {
        if((($start_position = strpos(strtolower($content), strtolower($words_array[0]))) !== false)
        and ($start_position > $backtrack)
        and ((($start_position - $backtrack) + $length) < $strlen))
        {
            $start_position = $start_position - $backtrack;
        }
        else
        {
            $start_position = 200;
        }
        $content = substr($content, $start_position, $length + 80);
        $content = preg_replace(array('/^[^ ]*[ ]/', '/[ ][^ ]*$/'), array('', '...'), $content);
    }
    return preg_replace(
        '~\b('.implode('|', $words_array).'[a-zA-Z]{0,3})\b(?![^<]*[>])~is',
        '<b>$0</b>',
        $content
    );
}
?>

Link to comment
Share on other sites

So instead of

 

$start_position = $start_position - $backtrack;

 

you want something like

 

while ($start_position > 0) {
  if ($content{$start_position} === '.') {
    # Found a dot.  Probably the end of a sentence. 
    $start_position += 1; # So let's assume the sentence starts just after the dot
    break; # And finish the while loop
  }
  # Otherwise, go back one letter and keep looking
  $start_position -= 1;
}

 

That will keep searching backwards until it finds a dot, or until it reaches the start of the string.

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.