Not sure if this is PHP, but...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Not sure if this is PHP, but...

Post by ibanez270dx »

Hi,
I have an online page editor program that needs to be able to support images wherever the user specifies. In perticular, the user will be dealing with a textarea box in which they can enter plain text, but I need some sort of tag that would call the image where the user specifies in the textbox. For example, in the textarea box, the user can put something like this:

This is my page. Here is a picture of me [img]me.jpg[/img]

The pictures would be uploaded beforehand and stored in folder images/ from the current directory. I guess what I need is something like here on the forums when you a post a topic or reply. I'd like a button on top that would insert the tags... How can I do this?

Thanks,
- Jeff
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

That type of embedding tags is called BBCode. You can find parsers for it or code your own. Google it or seach these forums.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

You can put a javascript-driven WYSIWYG in the textarea, but that will actually insert html into the page instead of bbcode like you asked for. TinyMCE Editor and FCKEditor (that's fck-- no "u" in there :-D) are the only ones I know of.

To use bbcode, I think you have to write a parser in php... I'll leave that one up to somebody else to answer. :D
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Firstly you would need the button itself, that's a bit of HTML and then you need to attach from JS to it for appending the textarea etc.
Then when the textarea is submitted you need to use PHP to find the tag and perform whatever action based upon it.

Here's a bit of JS that may help

Code: Select all

<button type="button" onclick="appendWithImgTag(this)">Add Img</button>
<script type="text/javascript">
function appendWithImgTag(this)
{
    var textarea = document.getElementById('the id here');
    if (!textarea) throw 'Not found';
    if(appendWithImgTag.open) {
        textarea.value += '[img]';
    } else {
        textarea.value += '[/img]';
    }
    appendWithImgTag.open = !appendWithImgTag.open;
}
appendWithImgTag.open = true;
</script>
untested. This isn't that greater code but meh.
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post by ibanez270dx »

I think I'd be able to do it using PHP... couldn't I use the str_replace() function? I just don't know how I can get the text in between the tags to a variable... I was thinking in this way:

where $name is the content between the tags...

Code: Select all

$content1 = str_replace('[img]', '<img src=images/$name>', $textarea_content);
$content2 = str_replace('[/img]', ' ', $content1);
would something like this be workable?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Workable? yes

Nice security hole? oh yeah baby.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

<img src=images/$name>
QUOTE! YOUR! HTML! ATTRIBUTES!
Seriously, it would make me very happy

Code: Select all

<img src="images/$name" alt="" />
ahhh, much better now.
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post by ibanez270dx »

cool - its not a public system anyway, so I don't have to worry about security problems with this part of it. Thanks everyone! I'll do my best with this... still, how would I get the text in between the tags into a variable?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

2 × strpos() + substr() or preg_match() if you're daring.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

just because it's not public doesn't mean you can throw security out the window.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

preg_match(). Or you can download phpBB and check out their bbcode.php page for how they handle bbcode.

Another nice bbCode parser is the one on Corz Blog. It is a little rough on the code to test, but once you get it, it is pretty nice.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Everah wrote:Or you can download phpBB and check out their bbcode.php page for how they handle bbcode.
Moments after opening said file ibanez270z will of course Image
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That was a good one. I have opened that file a few times and closed it short of getting the complete gist of it. But a simple glance can steer one in the right direction, if they can see what match/replace code is between all the gobbledygook.
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post by ibanez270dx »

With a little modification, the one on Corz Blog works perfect!!

Thanks everyone for your help!!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What did you do to the Corz Blog code? I haven't had time to completely finish tinkering with it.
Post Reply