I have a string that contains several blocks of text inside [], example: "the gray fox [is running] from the tiger [a hungry tiger]....etc.." I would like to display the text inside the [] as bold. NOTE: this data comes from a DB and in order to distinguish it, I need to make it bold or a different color.
I would do it something simple like: $string= "the gray fox [is running] from the tiger [a hungry tiger]....etc.."; $string= str_replace("[","<b>",str_replace("]","</b>",$string)); I don't know how efficient it would be but it works.
<?php function tobold($text){ return ereg_replace('\[([^\[]*)\]','<b>\1</b>',$text); } //Testing echo tobold("This is [Bold]<br />"); echo tobold("This is [Bold] with nesting error]<br />"); echo tobold("This is [Bold] and [onther bold]"); ?> PHP: This outputs: This is Bold This is Bold] with nesting error This is Bold and onther bold Note: This is basic because i'm still a noob; I hope it will help you