Bold Words That Start with Letter

(PHP) How do I make bold words from a text, that start with a specific letter?

This is easy enough to accomplish with preg_replace_callback(). You almost had it right with the regex -- note that you don't need to have capture groups for \b word boundaries (not the least since they are "non-items" and would only be captured as blanks!). This would do it:

$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';

$string = preg_replace_callback('/\b(d\w+)\b/i', function($match) {
return "<b>{$match[1]}</b>";
}, $string, -1, $count);

This function conveniently takes a referenced argument returning the number of replacements done. I have also added in the i modifier for case-insensitive matching, in case your lipsum should start with Dolor. This results in the following variables:

$string = "Lorem ipsum <b>dolor</b> sit amet, consectetur adipiscing elit, 
sed <b>do</b> eiusmod tempor incididunt ut labore et <b>dolore</b> magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.";

$count = 3;

Edit: Not sure why I initially wrote this answer using a callback... You can also accomplish this, including getting the count, with the simple preg_replace(), as follows:

$string = preg_replace('/\b(d\w+)\b/i', '<b>$1</b>', $string, -1, $count);

In any case, I'm leaving the callback version in place, in case you need to do something more complex with the result (e.g. also log replaced words for reference).

React Native add bold or italics to single words in Text field

You can use <Text> like a container for your other text components.
This is example:

...
<Text>
<Text>This is a sentence</Text>
<Text style={{fontWeight: "bold"}}> with</Text>
<Text> one word in bold</Text>
</Text>
...

Here is an example.

how to bold words within a paragraph in HTML/CSS?

<p><strong>This is in bold.</strong> This is not.</p>

You might find Mozilla Developer Network to be a very handy and reliable reference.

Bold the first two words from a sentence

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;

autocomplete how to write bold letters in suggestions

this is the main part that compares input value against the array:

if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase())

You just need to change it to the following to check if the array item contains the key string:

if ( (arr[i].toUpperCase()).indexOf(val.toUpperCase()) != -1)

Also to make the keyword bold:

current code:

 b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);

Alternative solution which I think is more simple even for the original post:

 b.innerHTML = arr[i].replace(val,"<strong>" + val + "</strong>");

in Order to ignore uppercase differences you may compare values in Lowercase and then use this answer to make first letter Uppercase again::

 b.innerHTML = (arr[i].toLowerCase()).replace(val.toLowerCase(),"<strong>" + val + "</strong>");

italicizing the first letter but NOT the rest of the letters of a word in text() in R

Try pasting the 'p' and '-value' together within expression():

text( 2.5, 9.5, expression(paste(bolditalic("p"),bold("-value"))), cex = 1.5)


Related Topics



Leave a reply



Submit