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>

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.