Jump to content
xisto Community
Sign in to follow this  
Jens_L

PHP And Texts: Need Help With PHP String Functions

Recommended Posts

Hey people!I'm pretty new with PHP and MySQL. I wanna have all the text on my site in MySQL. But i'm having trouble with the inputing from HTML-forms, like <textarea>.How to i do it?Can i use PHP or is it better to use javascript and sorta add htmltags into the <textarea> tag?I wanna make a texteditor where i can use bold, italic and that kinda stuff.I'm thankful for any help.../Jens

Edited by miCRoSCoPiC^eaRthLinG (see edit history)

Share this post


Link to post
Share on other sites

Not really sure about what you are trying to do but I think from your post that you r site will have textareas (which are part of an HTML form) and you want to store the data that is in these areas in a MySQL database. So you need to look at the striptag() PHP function and also the htmlspecialchars() PHP function at the links provided.

You will need to be a little more specific on how you are going to set up your site for anyone to really help you do what you are wanting to do and learning about PHP will help you out as well as learning some basic MySQL queries, it sounds like you are starting from the word go.

Share this post


Link to post
Share on other sites

Of course not houdini, the guy wants to have a texteditor that can use bold italic and stuff. With stripping tags you lose the markup information in the text.

Anyway, to have a php script take input from a html page with a textarea or textfield. You first need to have a webpage, maybe something like this:

<html><form name="foo" action="post.php" method="post"><textarea name="txt"></textarea><input type="submit" name="sub" value="Save">

Save this as a regular html file.

Now you need a script named post.php that does someting with the information.

<?php//this line reads the information that was send by the html file$txt = $_POST['txt'];// Now you need something that connects to a database and saves it// But since that's too much work, I'm gonna show that this works // and print the information on the screeecho($txt);?>

If you wanna save things in a database, check the php.net reference pages

http://nl3.php.net/manual/en/ref.mysql.php

Good luck with it.

Share this post


Link to post
Share on other sites

Thanks!

I know how to use php to store stuff in MySQL my problem is when i SELECT it and post it on my page. I don't seem to get all the newlines and stuff if i don't write html like <p></p> in the <textarea> tag.

 

I wanna do something like this texteditor i'm using right now

 

Thanks for ur help!

Share this post


Link to post
Share on other sites

First you would have to have a PHP that first takes the entered text that it can look at and render back in HTML, because this is what browsers use to convey the content of a page, since usually a text area would only produce the text as written to it ver batim then HTML tags or PHP code are meaningless.

 

So you would have to take the text entered into the text box and then running it through a parser of some sort and in this case you would use preg_replace and or str_replace that would be undestood by another script to render what you desire.

 

In the case of the Invision when you click one of the buttons like B I U it passes through the file in Invision board through 1.3.1 the source/post_parser.php file which would in turn parse the text and using preg_replace it would turn the BB or IB Code and turn it into HTML so that those viewing it would see the intended formatting. Below is an example of how this is done with just one simple tag like the bold tag.

 

$txt = preg_replace( "#\[b\](.+?)\[/b\]#is", "<b>\\1</b>", $txt );

then you have to do a couple of more things with this text after converting to HTML for display, this is when you want to edit the entered text that is in the database back to the origional BB Code or IB Code like so:

 

$txt = preg_replace( "#<b>(.+?)</b>#is"  , "\[b\]\\1\[/b\]"  , $txt );

Basically what you see above is code that allows PHP to parse some text and when it sees the bold tag both opening and closing it converts it to HTML so that a borwser will replace it with <b> then whatever is in between it and the </b> that it used the preg_replace() function and displays the intended result in the post.

 

Does this make any sense to you if not then I would be glad to help you with it but there are already gorum software programs that do this, phpBB does this a little differently and is harder to deal with, making it work with Invision is simple but with phpBB it takes some doing.

Share this post


Link to post
Share on other sites

So basically, you are trying to write a Bulletin Board Code Parser.

 

You want to have buttons that when clicked will transform the seleced text into the proper BBC to display as bold italic or underlined.

 

If you are outputing to a textbox, then after each line, you'll need to use the new line code \n

 

If you are outputting to the HTML body, you'll need to use the non-breaking space code  

 

That will get your line spacing correct. Otherwise everything will show on one line and wrap around to the next.

 

Now in your script, you'll need to use a switch to change between /n and   depending on whether or not you are outputting to a textbox or the the document body.

 

[/hr]

 

Now the parser creation that has been explained works like most forum BBC. It changes the

[b]Whatever[/b]
to
<b>Whatever</b>

That is all it does. You type the BBC in the textbox and the correct HTML is then displayed in the body.

 

The data is stored in the BBC form and parsed every time it is displayed in the document body.

 

[/hr]

 

This doesn't explain how to create the required buttons and links for your users to simply click on to surround the selected text a with the correct BBC.

 

For that, you need to use JavaScript.

This JavaScript would change all of the text in this forums post reply box to red.

java script: window.document.REPLIER.Post.value='[color=red]'+window.document.REPLIER.Post.value+'[/color]';window.stop();
Just type that into the address area of your browser and the text will be coded to red or create a link with this command:

<a href="java script: window.document.REPLIER.Post.value='[color=red]'+window.document.REPLIER.Post.value+'[/color]';window.stop();">Red</a>
Clicking on this link on a page with such a textbox would code the text in the textbox for the color red.

 

Instead of applying the BBC to all of the text in the textbox, you'll want to write a JavaScript link that will only encode the selected text.

 

I'm not going in to how this is all done, that would be better suited for the Javacript Forum instead.

 

I hope this begins to answer this rather general question that you have asked.

 

vujsa

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.