Jump to content

[SOLVED] How Would I Use str_replace To Complete This?


marcus

Recommended Posts

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>

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>";

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>";

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']);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.