Attach html to text

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
gr3gg0r
Forum Newbie
Posts: 5
Joined: Thu Nov 15, 2007 10:47 am

Attach html to text

Post by gr3gg0r »

I want to have a form that takes some text. Like a description, so it has to be able to be fairly long. I then want to write some code to add HTML formatting to the text. I want the HTML on either end of the text. So it will basically put the text into an HTML template. What are some ways I can do this? I'm fairly new to PHP and I can't seem to figure this out at all. Thanks for helping me out.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Something like this?

Code: Select all

<form method="post" action="php_page.php">
<textarea name="desc"></textarea>
<input type="submit" value="submit" />
</form>
and php_page.php:

Code: Select all

$desc = $_POST['desc']; //get description from form
$desc = '<span style="color: #FF0000;">' . $desc . '</span>'; //add html to $desc
echo $desc; //output $desc with new formating
gr3gg0r
Forum Newbie
Posts: 5
Joined: Thu Nov 15, 2007 10:47 am

Post by gr3gg0r »

That looks good to me, now what if I wanted it to create a csv file with that information in one cell?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Yeah. I'd really help you if I knew what csv was. :lol:

Oh, I get it. It is something for saving information, right? I know how to do that with XML. Does it have to csv?
gr3gg0r
Forum Newbie
Posts: 5
Joined: Thu Nov 15, 2007 10:47 am

Post by gr3gg0r »

a csv file is a "comma seperated values" file. Like a tab delimited file, but commas. An excel spreadsheet might work too but I prefer a simpler format. I don't know too much about XML but I would be willing to learn. Tell me about that and how it works. I'm sure I could do some research on my own so perhaps just tell me how to save stuff from a php web form like the one you gave as an example to an XML type file. I basically just need a way to get the information into a spreadsheet without laboriously doing a copy and paste. In the end this should have a lot of information going through it. Thanks!
sunilbhatia79
Forum Newbie
Posts: 24
Joined: Sun Nov 11, 2007 9:37 pm
Location: Mumbai, India

Post by sunilbhatia79 »

Are you looking at developing a script that will process HTML input put in the description by the user, or do you want to allow users to accept HTML input and then show it in another place?

Let me know so that I can help you with it.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Okay, you want to start out with an xml file like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<nodes>
  <node></node>
  <node></node>
  <node></node>
</nodes>
Each "node" tag is where the description will be.

Code: Select all

<?php
$xml = new DOMDocument(); //This starts up the DOM XML parser
$xml->load('nodes.xml'); //This loads the xml file

$nodes = $xml->getElementsByTagName('node'); //sets the var for each "node"

foreach ($nodes as $node){ //run through each "node"
  echo 'Description: <p>'. $node->nodeValue .'</p>'; //output the node
}
?>
That output all values in the xml document.

This is the php to add a new "node", with the description inside it:

Code: Select all

<?php
$desc = $_POST['desc'];
$xml = new DOMDocument();
$xml->load('nodes.xml');

$root = $xml->documentElement;

$new_node = $doc->createElement('node', $desc);
$new_node = $root->appendChild($new_node);

$xml->saveXML();
?>
gr3gg0r
Forum Newbie
Posts: 5
Joined: Thu Nov 15, 2007 10:47 am

Post by gr3gg0r »

sunilbhatia79 wrote:Are you looking at developing a script that will process HTML input put in the description by the user, or do you want to allow users to accept HTML input and then show it in another place?

Let me know so that I can help you with it.
I want the user to put in text, and get the text with the html added to it in some useable form.
In the end, I want the user to be able to put in a spreadsheet with any number of text field in one column and I want this to put out a spread sheet with that text with the html appended to it in one column.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you explain what you are after a little more clearly. I am having a little bit of a hard time trying to wrap my mind around you taking a text input, wrapping HTML around it then turning it into a CSV output.
gr3gg0r
Forum Newbie
Posts: 5
Joined: Thu Nov 15, 2007 10:47 am

Post by gr3gg0r »

Everah wrote:Can you explain what you are after a little more clearly. I am having a little bit of a hard time trying to wrap my mind around you taking a text input, wrapping HTML around it then turning it into a CSV output.
I basically want an easy way to get a text description (and perhaps a title) formatted with an HTML template. I'm pretty sure you can do what I want with excel but I want to create this with some enhancements in mind to be made in the future. I just need to get started right now.
Post Reply