Page 1 of 1
Not sure if this is PHP, but...
Posted: Sun Aug 20, 2006 8:35 pm
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
Posted: Sun Aug 20, 2006 8:41 pm
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.
Posted: Sun Aug 20, 2006 8:42 pm
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

) 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.

Posted: Sun Aug 20, 2006 8:44 pm
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.
Posted: Sun Aug 20, 2006 9:06 pm
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?
Posted: Sun Aug 20, 2006 9:08 pm
by feyd
Workable? yes
Nice security hole? oh yeah baby.
Posted: Sun Aug 20, 2006 9:14 pm
by Ollie Saunders
QUOTE! YOUR! HTML! ATTRIBUTES!
Seriously, it would make me very happy
ahhh, much better now.
Posted: Sun Aug 20, 2006 9:21 pm
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?
Posted: Sun Aug 20, 2006 9:24 pm
by feyd
2 ×
strpos() +
substr() or
preg_match() if you're daring.
Posted: Sun Aug 20, 2006 9:25 pm
by Luke
just because it's not public doesn't mean you can throw security out the window.
Posted: Sun Aug 20, 2006 11:02 pm
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.
Posted: Sun Aug 20, 2006 11:05 pm
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

Posted: Sun Aug 20, 2006 11:12 pm
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.
Posted: Tue Aug 22, 2006 10:14 am
by ibanez270dx
With a little modification, the one on Corz Blog works perfect!!
Thanks everyone for your help!!
Posted: Tue Aug 22, 2006 10:47 pm
by RobertGonzalez
What did you do to the Corz Blog code? I haven't had time to completely finish tinkering with it.