change text face in string

Discussion in 'PHP' started by jjhernandez, Jul 11, 2007.

  1. #1
    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.
     
    jjhernandez, Jul 11, 2007 IP
  2. Nefarious

    Nefarious Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    Nefarious, Jul 11, 2007 IP
  3. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #3
    <?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
     
    nabil_kadimi, Jul 11, 2007 IP
  4. jjhernandez

    jjhernandez Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks a lot, both suggestions worked like a charm!
     
    jjhernandez, Jul 12, 2007 IP