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> Link to comment https://forums.phpfreaks.com/topic/47408-solved-how-would-i-use-str_replace-to-complete-this/ 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>"; Link to comment https://forums.phpfreaks.com/topic/47408-solved-how-would-i-use-str_replace-to-complete-this/#findComment-231319 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>"; Link to comment https://forums.phpfreaks.com/topic/47408-solved-how-would-i-use-str_replace-to-complete-this/#findComment-231328 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']); ?> Link to comment https://forums.phpfreaks.com/topic/47408-solved-how-would-i-use-str_replace-to-complete-this/#findComment-231334 Share on other sites More sharing options...
marcus Posted April 17, 2007 Author Share Posted April 17, 2007 Works perfectly, thanks:) Link to comment https://forums.phpfreaks.com/topic/47408-solved-how-would-i-use-str_replace-to-complete-this/#findComment-231340 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.