marcus Posted April 17, 2007 Share Posted April 17, 2007 I am making a forums system, and I wanted the ability to have users be able to quote people. I have a little javascript thing, that would input: [quote=username]their message[/quote] into the text box. How would I implement str_replace to make it show up as: USERNAME said:<br> <div id="quote">THEIR MESSAGE</div> Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 17, 2007 Share Posted April 17, 2007 The "quote=username..." bit is coming in to your script via a POSTed TEXTAREA, and you want to display it in HTML, right? I'd use preg_match. preg_match('/[quote=(\w+)](.*?)[\/quote]/',$_POST['your_text_box'],$matches); // ... echo "$matches[1] said:<br><div id=\"quote\">$matches[2]</div>"; Quote Link to comment Share on other sites More sharing options...
marcus Posted April 17, 2007 Author Share Posted April 17, 2007 Ok, I've tried that, and it didn't work. $matches = "[quote=marcus]dood you're so sexy[/quote]"; $matches = preg_match('/[quote=(\w+)](.*?)[\/quote]/',$_POST['your_text_box'],$matches); echo "$matches[1] said:<br><div id=\"quote\">$matches[2]</div>"; Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 17, 2007 Share Posted April 17, 2007 Ah, you're right. I forgot to escape the square brackets; they're special characters. In fact, you can use preg_replace instead, provided you don't need to do anything else. <?php echo preg_replace('/\[quote=(\w+)\](.*?)\[\/quote\]/','$1 said:<br><div id="quote">$2</div>',$_POST['your_text_var']); ?> Quote Link to comment Share on other sites More sharing options...
marcus Posted April 17, 2007 Author Share Posted April 17, 2007 Works perfectly, thanks:) Quote Link to comment 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.