Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display <textarea> content without loosing its integrity - How? 1

Status
Not open for further replies.

josel

Programmer
Oct 16, 2001
716
US
First of, I have to thank this community as it has been invaluable for the past few weeks. I just noticed that my first post dates back to Jul 08, 2007; I wanted to know how to install and configure PHP. A week later, I posted a question wondering "How do I know PHP is running?" ... Time and again, you guys have come through and delivered an answer, and for that, I am very grateful. A couple of weeks later, this is my progress report: Thank you all !!!

Now on with my new problem!

My problem can be best explained if you visit this link
Code:
[URL unfurl="true"]http://www.fpgroups.com/index.php?Target=code&Action=read&Code=6[/URL]

Click on Description and Code tabs. As you can see, the content does not appear clean. Within the description, I have > characters that I do not know where they come from. In the code tab, the content looks just as weird.

Here is the thing, In the description tab, if the content has HTML, I want those tags to resolve as they normally do, I am not interested in suppressing them. In the code tab, I want the total opposite. In both tabs, I want to maintain new lines (do not want every text cluttered together).

How do I go about this?

Thank you all in advance,


Jose Lerebours




KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
in the description you are including a second > after you <br>, this will be a problem in your rendering code. just a trailing close tag

I am not sure how the CODE tab is supposed to look.
 
Hi

You wrote "<textarea>", but a search through the HTML in the View Source window found no [tt]textarea[/tt] tag.

Anyway, we neeed to see what you done in the code. The > signs would say you a minor mistake somewhere, but the code says nothing to me.

What did you used to get that output ? [tt]htmlspecialchars()[/tt], [tt]strip_tags()[/tt], other ?

Feherke.
 
Here is the code for the link in question
Code:
<?PHP
function Array2String($TheArray) {
// This function receives an array as parameter, loops through every 
// element and creates a long string by combining every element into
// a single line and separating them with <br /> to for a line break

$string = "";
$size = sizeof($TheArray); 
for($i = 0; $i < $size; $i++){
	$string = $string . $TheArray[$i] . "<br>";
}
return $string;
}
?>

<?PHP

function ReadSnips()
{
//variables for connecting to MySQL
$mysql_host = 'the-host';
$mysql_user = 'the-user';
$mysql_pass = 'the-password';
$mysql_db   = 'the-database';

//connect to MySQL
mysql_connect ($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db ($mysql_db);

//query the database for the required records
$data_query = 'select * FROM `snips` WHERE `snips`.`snipID` = ' . $_GET['Code'] .  ' LIMIT 1';
$rh = mysql_query ($data_query);
$row = mysql_fetch_array($rh);

if (sizeof($row) > 0) {

$vAuthor = "";   //$row['snipAuthor'];
$vSince = "";  //$row['snipDate'];
$vSubject = $row['snipSubject'];
$aDesc = explode("<br /",nl2br($row['snipDesc']));
$vDesc = Array2String($aDesc);
// $vDesc = html_entity_decode(wordwrap($row['snipDesc'], 66, "<br />\n"));
$aCode = explode("<br />",nl2br($row['snipCode']));
$vCode = Array2String($aCode);

$CommRowTable = "";
$CommQuery = 'select * FROM `snipscomments` WHERE `snipscomments`.`snipID` = ' . $_GET['Code'];
$CommData = mysql_query ($CommQuery) or die (mysql_error());

if (mysql_num_rows($CommData) > 0) {
    $CommRowTable = '<table border="1" cellpadding="0" cellspacing="0">';
    while ($crow = mysql_fetch_assoc($CommData)) {
        $aSnipComment = explode("<br />",nl2br($crow['snipComment']));
        $vSnipComment = Array2String($aSnipComment);
        $CommRowTable .= '
            <tr><td bgcolor="#CCCCCC"><span style="float: left; width: 330px;">Posted By:&nbsp;' . $crow['snipCommBy'] . '&nbsp;&nbsp;|&nbsp;&nbsp;On:&nbsp;' . $crow['snipCommDate'] . '</span></td></tr>
            <tr><td>' . $vSnipComment . '</td></tr>
        ';
    }  // Close while loop
    $CommRowTable .= '</table>';
} else {
    $CommRowTable = '<table border="0" cellapdding="0" cellspacing="0"><tr><td>No Comments Posted!</td></tr></table>';
}  // Close if no records


echo '
<table name="tabTable" border="0" cellpadding="0" cellspacing="0">
<tr><td>
<div class="tabber">
     <div class="tabbertab">
	  <h2>General</h2>
	  <p>
	  <table name="tabGeneral">                 // THE GENERAL TAB
		  <tr>
		  	<td>Posted By</td>
			<td>' . $vAuthor . '</td>
		  </tr>
		  <tr>
		  	<td>Posted Date</td>
			<td>' . $vSince . '<td>
		  </tr>
		  <tr>
		  	<td colspan="2">&nbsp;</td>
		  </tr>
		  <tr>
		  	<td colspan="2">' . $vSubject . '</td>
		  </tr>
	  </table></p>
     </div>
     <div class="tabbertab">                      // THE DESCRIPTION TAB
	  <h2>Description</h2>
	  <p>' . $vDesc . '</p>
     </div>
     <div class="tabbertab">                     // THE CODE TAB
	  <h2>The Code</h2>
	  <p>' . $vCode . '</p>
     </div>
     <div class="tabbertab">                     // THE COMMENT TAB
	  <h2>Comments</h2>
		<form action="AddComment(\'addcodecomments.php?Code=1\')" method="post"><table><tr><td><input type="button" onClick="AddComment(\'addcodecomments.php?Code=1\')" value="Add Comments"></td></tr></table></form>
	  <p>' . $CommRowTable . '</p>
     </div>
</div>
</tr></td>
';

} else {
print '<table width="100%" height="100%"><tr><td>Unable to read record!</td></tr></table>';
}
print '<table width="100%"><tr><td><a href="' . $_SERVER['PHP_SELF'] . '?Target=code' . '&Action=add"><img src="images/addsnip.jpg" border="0"></a></td></tr></table>';


}   // End of function ReadSnips() //
?>

I have tried a number of tags html_special_character() and others listed at php.net associated with it ... I am not getting the results I am looking for.

I mentioned <textarea> because when I collect the information, I use <textarea> for input. I store the data in a mySQL table which I then show on this page. If the user presses ENTER and leaves a blank line, I want to show that blank line. Using nl2br() double spaces the content thus making it look kind of "foolish?" shall I say ...

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
change this line as shown
Code:
$aDesc = explode("<br /[red]>[/red]",nl2br($row['snipDesc']));

but personally i'd get rid of the Array2String functin. you're just duplicating the function of nl2br().
 
I need to walk away from this code for a day or two. I am so into finishing this site that I can't see straight.

I must to read this routine over 100 times last night ... I just did not see that missing.

Thank you very much!

I am now faced with neatly displaying content that hast HTML code in it and not suppress these characters. I am reading through php.net ... Hope to find an answer ...

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
when you say html code is it that you want the box to have the raw html code

i.e. the user sees
Code:
<b>hello world</b>

or do you want the user to see the words "hello world" in bold text?

if the former, use htmlspecialchars() before echoing the string.
 
jpadie,

If I use htmlspecialchars(), and I already tried it, everything shows in a huge single block of text.

I need to retain new lines ... Here is an example of the kind of things people will be 'dumping' on this site

Code:
::declare extern FLOutFile 'Pass output location in this variable.:
::declare extern FLErrOut 'Check open return with this variable.:
::declare Msg,FLResult,OHandle(4,.0):
::OHandle=open(FLOutFile,"wc0t"):
:OHandle lt "0":FLErrOut = "FAIL";end:
::FLErrOut = "PASS":
::Msg="":
:':'***** Beginning of embedded HTML *****:
::Msg=Msg&"<html>":
::gosub sendln:
::Msg=Msg&"<head>":
::gosub sendln:
::Msg=Msg&"<title>Fairlight HTML to filePro PRC Converter Documentation</title>"
:
::gosub sendln:
::Msg=Msg&"<style>":
::gosub sendln:
::Msg=Msg&"body {":
::gosub sendln:
::Msg=Msg&"     scrollbar-face-color^A #AAAAAA;":
::gosub sendln:

[ ... Further tonnes of HTML code broken into segments ... ]

::Msg=Msg&"<span class=\"copyright\">Copyright 2004-2007, Fairlight Consulting.  All rights r":
::Msg=Msg&"eserved.</span>":
::gosub sendln:
::Msg=Msg&"<br><br><center><a href=\"http^A//[URL unfurl="true"]www.fairlite.com/\">Back[/URL] to Fairlig
ht Consulting<":
::Msg=Msg&"/a>":
::gosub sendln:
::Msg=Msg&"</body>":
::gosub sendln:
::Msg=Msg&"</html>":
::gosub sendln:
:':'***** End of embedded HTML *****:
::end:
sendln::Msg=Msg&chr("10");FLResult=write(OHandle,Msg,len(Msg)):
::Msg="":
::return:

The site I am writing is intended to serve as a FORUM for a group of developers that use this little known 4GL application ( Most of these developers (including yours truly) work in UNIX and use mostly vi as the text editor of choice.

Looking at what I posted here, I cannot understand why it looks the way it does here



Thanks,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
If I could only show it the way it does here I will be a very happy camper !!!


Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
oh. ok. try this code. Just a simple substitution.

Code:
function cleanInput($code){
 $_code = str_replace(array('<br />', '<br/>','<br>','<BR/>', '<BR />', '<BR>'), 'NEWLINE', $code);
 $_code = htmlspecialchars($_code);
 return str_replace('NEWLINE','<br />', $_code);
}
 
Excellent !!!! :) :) :)

How many stars can I give you???

Thank you so very much ...

I could go on but I think you get it :)

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top