surfwtw Posted December 29, 2011 Share Posted December 29, 2011 I want a code to automatically make specific words within my content bold. For example, If the word "car" appears, make that word bold. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/ Share on other sites More sharing options...
Pikachu2000 Posted December 29, 2011 Share Posted December 29, 2011 Where are you stuck? Post your code and tell us what problems you're having with it. Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1302366 Share on other sites More sharing options...
AyKay47 Posted December 29, 2011 Share Posted December 29, 2011 most likely str_replace, or for more complex patterns, preg_replace Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1302370 Share on other sites More sharing options...
QuickOldCar Posted December 30, 2011 Share Posted December 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1302384 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2011 Share Posted December 30, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1302392 Share on other sites More sharing options...
QuickOldCar Posted December 30, 2011 Share Posted December 30, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1302394 Share on other sites More sharing options...
surfwtw Posted December 30, 2011 Author Share Posted December 30, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1302606 Share on other sites More sharing options...
QuickOldCar Posted December 30, 2011 Share Posted December 30, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1302624 Share on other sites More sharing options...
QuickOldCar Posted January 7, 2012 Share Posted January 7, 2012 wrong post Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1305311 Share on other sites More sharing options...
Unclickable Posted January 8, 2012 Share Posted January 8, 2012 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1305444 Share on other sites More sharing options...
QuickOldCar Posted January 8, 2012 Share Posted January 8, 2012 \b is a word boundary to see the other escape sequences http://de2.php.net/manual/en/regexp.reference.escape.php Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1305455 Share on other sites More sharing options...
QuickOldCar Posted January 8, 2012 Share Posted January 8, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1305482 Share on other sites More sharing options...
jcbones Posted January 8, 2012 Share Posted January 8, 2012 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. It is a regular expression pattern. RegEx is complicated, but will match any string you need. RegEx tutorial. Quote Link to comment https://forums.phpfreaks.com/topic/254045-what-is-a-php-code-to-automatically-bold-certain-words-within-my-content/#findComment-1305516 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.