Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To Be So Bold....

Status
Not open for further replies.

EvilAsh

Technical User
Oct 22, 2005
56
GB
This may not be possible!

If I extract text from a mysql database using php, is it possible to embolden specific words?

For example, if the mysql value was: "hello, how are you?", would I be able to embolden the word "are" leaving the rest in the default style?

Thanks for any help you can offer.
 
Your problem is just about the same as the one discussed (and solved) here: thread434-1215130
 
Thanks for the reply.

Hmmm, I think I am looking at a different problem. I want to avoid having to preselect the words in code and try to select thenm on the fly.

I guess what I am trying to simulate is the way forums (such as this) render text in posts like so by surrounding the word with tags.

I appreciate that this is done using TGML on this forum but I wonder if this can be done in php.
 
sure. php has bbcode parsers but somewhere the search terms/tgml tags/bbcode are always preselected.
 
I don't know how you plan to decide which words you want to embolden (that's between your PHP code and your database), but once you know, you can just add the html tag around the words when you send it out. The literal way would be:
Code:
echo "Hello, how <b>are</b> you?";
Yes, I know that example is hardcoded, but hear me out. You can make that same text happen dynamically in a variety of ways. If, for example, you had an array with a list of words you wanted to embolden, and $mytext was a variable holding some text you got from your database that you want to display, you could do something like this:
Code:
for (i=0; i<count($array); i++) {
  $mytext = ereg_replace($array[i],"<b>".$array[i]."</b>",$mytext);
}
echo $mytext;
Does that example help you see the possibilities?
 
Addendum: I just read jpadie's reply to a very similar post ("Highlight particular words") and he used some cleaner code than I did, like the foreach statement instead of a for loop and str_replace rather than ereg_replace (faster for simple string replacements that don't need "regular expressions"). But anyway, hopefully you get the idea.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top