GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jan-2005, 02:44
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
Question

working with html HELP


I'm trying to wrap my head around working with html text but I cannot grasp how it all works. Ok so I in the following code I have entered the contents of a html doc into an array and been experimenting with the different functions.


PHP Code:

function format_html($method, $htmlstring)
{
 switch ($method)
 {
         case "specialchars":
               $result = htmlspecialchars($htmlstring, ENT_QUOTES);
               break;
         case "stripped":
               $result = strip_tags($htmlstring);
               break;
 }
 return $result;
}

$line2 = array();
$filePointer = fopen("data.html", "r");

while (!feof($filePointer))
{
              // add to array
             $line2[] = fgets($filePointer, 4096);
}
fclose($filePointer);

// print formated html from first line of file, first entry in array
print format_html("specialchars", $line2[0]);

print "</textarea><br><br>\n\n"; 



If I now have converted all html characters to their entity reference values it displays the html correctly so as not to be executed in a textarea.
So now If I edit the html in the textarea using normal html characters (quotes etc) will the data be converted when I send the data?

And how would I convert the character entities back to their original html characters? I'm sure theirs a function that does this.
Thanx in advance
peace
  #2  
Old 03-Jan-2005, 13:57
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
Great I found a resource that deals with this nicely!
za2.php.net showed how to use html_entity_decode, that function name I was missing.

What is confusing me now is how the html text is collected and dealt with from a form element, ie a textarea field.
So heres the situation: a user enters html text into a textarea, including quotes and things similar, and that info is sent as a text string right?
The info would need to be edited before being sent right? Like \ needs to be placed before the quotes so that php can store the html text in a varaible. So i donno how to do this.

Can anyone please help me? Or does anyone know where I could find examples of what I am trying to do here (work with html text from a form) it would be a huge help i'm really struggling with this.
peace
  #3  
Old 03-Jan-2005, 14:57
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
Well, I seem to have solved it. Why did no one say that html text was formatted (addslashes) as it is sent?
It was pretty easy to figure it out at the end of the day.
All I had to do www.spliffsa.com Go ahead try it and view the html source of the results page. There's those sneaky backslashes that confused the hell outa me.
And the php is
PHP Code:

<?
print $textvalue;
?>

just to see what was happening.
  #4  
Old 03-Jan-2005, 20:08
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dopee
Well, I seem to have solved it. Why did no one say that html text was formatted (addslashes) as it is sent?
Probably because you answered your questions before we got to them

Quote:
Originally Posted by dopee
It was pretty easy to figure it out at the end of the day.
All I had to do was this. Go ahead try it and view the html source of the results page. There's those sneaky backslashes that confused the hell outa me.
And the php is
PHP Code:

<?
print $textvalue;
?>

just to see what was happening.
Do you understand where those pesky backslashes come from? Or is that still a mystery?
__________________

Age is unimportant -- except in cheese
  #5  
Old 04-Jan-2005, 02:30
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
Well i'm sure has something to do with the "magic_quotes" or sommin similar setup withing the php.ini file?
Cos on one linux server it automatically adds slashes, yet on a different linux server it doesn't.
So I do this to convert line breaks and deal with quotes:
nl2br(addslashes($textvalue))
It's all coming together now...
peace
  #6  
Old 04-Jan-2005, 08:51
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
I realise that I may be too late but I'll add my comments nevertheless:

I don't know why you would create a CUSTOM function to do what is already available in PHP. For example, I would re-write the example code in your first post like this...

PHP Code:

// the custom function removed from here

$line2 = array();
$filePointer = fopen("data.html", "r");

while (!feof($filePointer))
{
              // add to array
             $line2[] = fgets($filePointer, 4096);
}
fclose($filePointer);

// print formated html from first line of file, first entry in array
         // print format_html("specialchars", $line2[0]);
         print htmlentities( $line2[0], ENT_QUOTES );

print "</textarea><br><br>\n\n"; 



Quote:
And how would I convert the character entities back to their original html characters?

You don't need to do anything in this situation - trust me! For example if you view the source code of your <textarea> and if it looks like this:

HTML Code:
<textarea name="some">&lt;span&gt;Something&lt;/span&gt;</textarea>

and if you then submit this form, the receiving script has this string in the $_POST['some'] variable : <span>Something</span>. Strange but true...!

About magic_quotes_gpc, it's a setting inside php.ini file and if it's ON, all GET / POST / COOKIE operations & variables are escaped for (quotes, backslashes and NULs) automatically. There are opinions about this setting and many cool ideas to make your scripts deal with either situation without modifying your code. Here's just one, I wrote a LONG time ago: http://www.gidforums.com/t-380.html.
  #7  
Old 05-Jan-2005, 08:57
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
thanx for the help JdS

Quote:
and if you then submit this form, the receiving script has this string in the $_POST['some'] variable : <span>Something</span>. Strange but true...!

Yes I see this now...its kinda hard to grasp how a variable (eg some) can contain html elements, like quotes, but understanding that makes this a whole lot easier. Like even with the html being encoded it'll send it decoded (as you mentioned). I realise more and more the easiest way to learn code is through trial and error. It's been a while since my turbo pascal days!

My main point to learning this is to simply display all the html text from a html file in a textarea. (So there really is no need to encode the text.) And then to save the contents of the textarea into the same html file.
After learning how to encode and decode I realise I don't really need to do it!

Last thing I need to ask is about the _POST variables. I've realised now you work with your form variables different to what i've taught myself.
Like instead of using $_POST['some'] I would just use $some. What exactly is the difference?
thanx a lot for the help
peace, rich
  #8  
Old 06-Jan-2005, 07:36
cdhouse.biz cdhouse.biz is offline
New Member
 
Join Date: Jan 2005
Posts: 23
cdhouse.biz is on a distinguished road
here u go
http://hk.php.net/manual/en/language.variables.external.php
__________________
http://www.cdhouse.biz
Cheap Linux, FreeBSD, OpenOffice CDs
  #9  
Old 07-Jan-2005, 05:12
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
To quickly answer your question about using $_POST['some'] instead of $some, I will just quote something found on the page that cdhouse.biz has linked to in his reply:

Quote:
Note: Superglobal arrays, like $_POST and $_GET, became available in PHP 4.1.0

As shown, before PHP 4.2.0 the default value for register_globals was on. And, in PHP 3 it was always on. The PHP community is encouraging all to not rely on this directive as it's preferred to assume it's off and code accordingly.
  #10  
Old 17-Jan-2005, 04:18
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
so its better to use instead $some instead of $_POST['some']?
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What should I use? XHTML 1.0 or HTML 4.01 Strict? retxedz Web Design Forum 0 24-Dec-2004 15:56
how do i make it so the image around my html slice expands when typed in ? gameshow863 Web Design Forum 6 03-Jun-2004 05:48
Getting HTML Source with PHP Optical MySQL / PHP Forum 2 18-May-2004 16:57
html to php tenaki Web Design Forum 17 28-Oct-2003 16:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.