tags:

views:

189

answers:

3

Hello guys,

In PHP, how can I bold the first two words from a sentence?

Thank you!

+3  A: 

You need to break things down into steps...

1) You have a sentence, like this:

$Sentence = "Hello everybody in the world.";

2) You need to get the first two words. There are two options. You can either split the sentence on every space, or you can find the position of the second space. We'll use the first option for now...

$Words = explode(" ", $Sentence);

3) We re-assemble it all, inserting a bit of HTML to make things bold...

$WordCount = count($Words);
$NewSentence = '';
for ($i = 0; $i < $WordCount; ++$i) {
    if ($i < 2) {
        $NewSentence .= '<strong>' . $Words[$i] . '</strong> ';
    } else {
        $NewSentence .= $Words[$i] . ' ';
    }
}
echo $NewSentence;
Sohnee
Thanks Sohnee, this seem to work.
Psyche
auch! That would output `<strong>Hello</strong> <strong>everybody</strong> in the world.` (apart from your typo on not closing the strong tag).
peirix
... which would look just fine in the browser.
Thilo
using the "limit" parameter in the function explode you can skip the loop and make your code much simpler... see my solution below. An innecessary loop that opens and closes a bunch of tags is not a ellegant nor efficient solution...
danii
This solution is incomplete: what if the separator is not space but comma, exclamation mark or anything else?
chelmertz
I would suggest splitting using a regular expression instead.
Paul Lammertsma
This is not a good solution since it loops through the whole sentence, you should only cut off the first two words, wrap them in strong tags and concatenate the rest at once.
tharkun
chelmertz - the question is about the first two words in a sentence, even if there is punctuation, this would be followed by a space. In response to the performance comments - I must admit that I typically perform this kind of operation on the way into the database (using a span rather than a strong tag) - so it is only ever used on the insert - not on every read. You would need to be more careful if you're performing it on every read.
Sohnee
+10  A: 

Actually, using the "limit" parameter in the function explode (3rd parameter, optional, check the function spec) you can skip the loop and make your code much simpler:

$words_array = explode(" ",$sentence,3);
$new_sencence = ( count($words_array)>2 )? 
    "<strong>".$words_array[0]." ".$words_array[1]."</strong> ".$words_array[2] :
    "<strong>".$sentence."</strong>"; //sentence is 2 words or less, just bold it

EDIT: took care of sentences with 2 words or less

danii
It is a better solution I believe.
craftsman
It is indeed a better solution because you are not looping trough the whole text.
Marien
The code provided doesn't take care the possibility of having a one-word sentence.
Anax
+3  A: 

preg_replace('/^(\S+(\s+\S+)?)/', '<b>$1</b>', $sentence)

Marcelo Cantos
I prefer <strong>, but also this doesn't handle one-word sentences either.
Tom
Thank you for making some good points. On the first point, the question asked for bold, so that's what I provided. The question of whether `<strong>` is better is debatable, and I'm more coder than designer, so I steer clear of such issues.I've emended the regex to accommodate single-word sentences.
Marcelo Cantos