PHP form to parse html

Posted on 2006-04-14
Last Modified: 2013-12-16
My question is if there is a relatively easy way for my site to preserve xcel, word and other formatted documents, including html formatting. Right now my form removes all formatting and I was thinking perhaps that somebody has a function I could use or something...
Question by:rae_rae
    LVL 3

    Author Comment

    I've seen a program that somehow loads a blank html document into the form textarea...
    My form is laid out like this:
                // For news forms---------------------------------------------------------------------
                case 'form_news':
                echo '<table border="0"><tr><td><align="left">';
                echo '<form action="processor.php?sender=ins_news" method="post">';
                echo '<fieldset>';
                echo '<legend>Insert New News Section</legend>';
                echo 'News Title:';
                echo '<br><input type="text" name="content_name"><br>';
                echo 'News Body:';
                echo '<br><textarea rows="8" cols="30" name="content_body"></textarea><br>';
                echo 'Positon [100 shows before 200]:<br>';
                echo '<input type="text" size="3" name="content_position">';
                echo '<input type="Submit" value="Submit">';
                echo '<input type="Reset">';
                echo '</form>';
                echo '</fieldset>';
                echo '</td></tr></table>';
                $displ_query  = "SELECT content_name, content_position FROM News_Content ORDER BY content_position";
                $result       = mysql_query($displ_query);
                echo '<font size="2"><b>News Title and Section Position Already entered:</b></font><br>';
                while($row = mysql_fetch_array($result, MYSQL_ASSOC))
                            echo "<font size='2'> {$row['content_name']} {$row['content_position']} </font><br>";
                echo '<a href="processor.php?sender=cpanel">Back to Admin Panel</a>';
                // ----------------------------------------------------------------------------------
    of couse, make header actually contain the css and regular <html><body> and make_footer contains </body></html>
    LVL 3

    Expert Comment

    it is NOT POSSIBLE to maintain any external formatting inside a standard <textarea> element. This element could be styled, but after this, the style is uniform for the whole <textarea> element. it is POSSIBLE to create a special DHTML "textarea" to achieve this goal. Some online HTML editors use this strategy. You can use AJAX technology to achieve your goal, too (but this is a little bit more complicated).
    LVL 25

    Expert Comment

    (Or an applet)
    LVL 6

    Accepted Solution

    I'm not sure I fully understand you're question, but maybe it will become clearer as I try to answer. I have to assume that you have some web form that allows users to input data, or possibly upload files, and then it parses that data and stores it somewhere. IF you allow them to upload files (e.g. Word documents in the .doc format or Excel documents in the .xls format), while nothing is impossible, it's practically impossible to keep any of the proprietary formatting that DOC and XLS use. If you've ever looked at one of those files using a plain-text editor like Notepad, you'll quickly see that the true formatting used would be an absolute nightmare to deal with.

    However, if you're not uploading files and you're merely letting users input things on the web (for instance, in a textarea element as some people have mentioned), then the clear answer is to use a WYSIWYG input form. There are many of these freely available on the internet, I prefer this one myself:

    These use JavaScript to let users input "rich text," so that "what you see is what you get." If you have created a document in Word, you may literally copy/paste it into one of these WYSIWYG editors and will look exactly the same. Likewise with Excel. The JavaScript changes the Word or Excel format into HTML, and then if you submit that form to a PHP script, what you get is a bunch of text with HTML formatting. If you wanted ONLY the text part and none of the formatting, you could use PHP's built-in function strip_tags() to remove the HTML. Otherwise, the data would be saved along with any formatting, so that when you output it in the web browser, it would look exactly like it did in Word in Excel (more or less).
    LVL 3

    Author Comment

    How can I check for the existence of a string of characters within a php variable? Thanks!
    LVL 6

    Expert Comment

    There are many functions to do this sort of thing. Since there are myriad ways of "checking for existence," I'll name a few and briefly describe them.

    if ( isset($var) )
        // If $var has been assigned to literally anything at all

    if ( strlen($var) )
        // If $var has a "string length" greater than zero, this is true
        // This one is a stronger test than isset(), but please note
        // that integers have "string length" values as well. For instance,
        // strlen(29) == 2, because it would convert 29 to "29"

    if ( !empty($var) )
        // empty() checks to see whether the variable is empty or not. (duh)
        // This conditional is the negation of that.

    if ( is_string($var) )
        // For obvious reasons, this one is probably the most reliable for
        // checking whether $var is a string or not. This one's even stronger
        // than strlen(), because it would return FALSE if an integer was entered.
        // However, is_string("") returns TRUE (for the empty string). It does return
        // false for this, though: is_string(NULL)

    I frequently use "if ( strlen($var) )" to test for things, because it's nice and clean. It only requires that the input has length greater than zero. I'd recommend to use that one, anyway. If you really, really, really want to ensure that your input is not strictly a number, then use is_string, but otherwise strlen() is fine.

    Browse on over to They have a search engine on the top right that lets you look through every single function that is defined in PHP with elaborate explanations for each and a list of similar functions on the left side. It's really quite helpful.
    LVL 3

    Author Comment

    Thanks Soapergem for your invaluable help and willingness to explain things! I'll definately give all this a wirl and see what I can come up with.
    LVL 6

    Expert Comment

    Maybe you could give it a 'whirl' as well. ;)

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 67
    pre4 challenge 19 75
    Fix45 challenge 15 66
    changeXy challenge 13 40
    This article will show, step by step, how to integrate R code into a R Sweave document
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now