The Little Guy Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
btherl Posted April 19, 2007 Share Posted April 19, 2007 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)? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 19, 2007 Author Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted April 19, 2007 Share Posted April 19, 2007 For starters there is no such thing as bold text in PHP. How do you plan on defining these bold words? Quote Link to comment Share on other sites More sharing options...
btherl Posted April 19, 2007 Share Posted April 19, 2007 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? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 19, 2007 Author Share Posted April 19, 2007 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 ); } ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 21, 2007 Author Share Posted April 21, 2007 Thanks, It seems to work! Quote Link to comment 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.