Playing with str_replace() function

Discussion in 'PHP' started by gch5185, Nov 11, 2010.

  1. #1
    Hi.

    Recently, I'm playing with something related to BBCode in phpBB3. When I trace my database, the posts table and for a random post. I found that the image tag is written this way. There are 8 random characters generated between [img: ... ] for each post.

    My question is, how can I make use of preg_replace() to replace the random characters into this way..

     
    Last edited: Nov 11, 2010
    gch5185, Nov 11, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    instead of str_replace use this

    $your_bb_string = "[img:fcjsgy5j]";

    print_r(sscanf($your_bb_string, "[img:%s]"));
     
    bartolay13, Nov 11, 2010 IP
  3. gch5185

    gch5185 Peon

    Messages:
    114
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You don't get me..

    I want to convert [img:fcjsgy5j]http://imageurl.jpg[/img] to <img src="http://imageurl.jpg"> and fcjsgy5j is just a garbage value. Thanks.
     
    gch5185, Nov 11, 2010 IP
  4. irving

    irving Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?php
    
    function bb_img_to_html_img($string){
    	$pattern = '#\[img\:.+\](.+)\[\/img\]#iUs';
    	$replace = '<img src="$1" />';
    	return preg_replace($pattern, $replace, $string);
    }
    
    
    print("\n\nTag test [img:sldkjsd]:\n");
    $string = "[img:sldkjsd]http://www.somesite.com/image.jpg[/img]";
    print(bb_img_to_html_img($string));
    
    ?>
    PHP:
    This matches the [img:sometext] pattern only - it won't replace img tags where the garbage text doesn't exist.
     
    irving, Nov 12, 2010 IP
  5. gch5185

    gch5185 Peon

    Messages:
    114
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay irving, you just save my day. Million thanks!
     
    gch5185, Nov 12, 2010 IP