Jump to content

What is a PHP code to automatically bold certain words within my content?


Recommended Posts

There's always an issue with case when replacing, they can replace any match it finds, but sadly would then replace with whatever case your search word was.

Here is my solution.

 

<?php
function boldWord($word,$text){
$word = strtolower($word);
$ucf_word = ucfirst($word);
$uc_word = strtoupper($word);
return str_replace(array($word,$ucf_word,$uc_word), array("<b>$word</b>","<b>$ucf_word</b>","<b>$uc_word</b>"), $text); 
}

$text = "Let's replace car in the text, Car and CAR also replaces.";
echo boldWord("car",$text);
?>

 

Results:

Let's replace car in the text, Car and CAR also replaces.

 

If you don't care about the case, a simple str_ireplace would do.

$text = "Let's replace car in the text, Car and CAR also replaces.";
$word = "car";
echo str_ireplace($word, "<b>$word</b>", $text);

or preg_match

$text = "Let's replace car in the text, Car and CAR also replaces.";
$word = "car";
$pattern = "/$word/i";
$replace = "<b>$word</b>";
echo preg_replace($pattern, $replace, $text);

Results for both:

Let's replace car in the text, car and car also replaces.

Or if you only want to replace whole matching words -

 

<style type="text/css">
span.highlight {font-weight:bold;} 
</style>
<?php

$search =  "php|web"; // can be a single word or an OR'ed list of words

$string = "PHP is the web scripting language of choice, php, Web, phpweb";

$string = preg_replace("/\b($search)\b/i",'<span class="highlight">\1</span>',$string);

echo $string;

Yup, that's a good way too PFMaBiSmAd

 

A while back I made 2 different functions for a multi-word,multi-color highlighter

 

demo and code

 

In use at my random urls

example

Here is the code.  "$word" is the word that I want to be bold. "$definition" is all the content that is being displayed.  When "$word" appears in the content ($definition) I want "$word" to be bold.  Thanks

 

<h2><b>Definition of <?php echo "$word";?></b></h2>

</td>

</tr>

 

<tr>

<td cellpadding="5">

<?php echo "$definition"; ?> </center></br></br>

The answers were already written in the previous posts, but here is the code using PFMaBiSmAd's method

 

<style type="text/css">
span.highlight {font-weight:bold;} 
</style>

<h2><b>Definition of <?php echo "$word";?></b></h2>
</td>
</tr>

<tr>
<td cellpadding="5">
   <?php echo preg_replace("/\b($word)\b/i",'<span class="highlight">\1</span>',$definition); ?> </center></br></br>

 


   <?php echo preg_replace("/\b($word)\b/i",'<span class="highlight">\1</span>',$definition); ?> </center></br></br>

 

Hey I am kind of new to this and have the same problem. But could someone explain to me what the "/\b($word)\b/i" part of the code means? Sorry for the newbiness.  :shrug:

I bet you thought it might have meant bold, as in html's <b></b>, but it isn't.

 

An easy way to highlight without using the css style

echo preg_replace("/\b($word)\b/i", "<b>\1</b>", $string_of_text);

 

 

Hey I am kind of new to this and have the same problem. But could someone explain to me what the "/\b($word)\b/i" part of the code means? Sorry for the newbiness.  :shrug:

 

It is a regular expression pattern.  RegEx is complicated, but will match any string you need.

RegEx tutorial.

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.