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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing formatting from text

Status
Not open for further replies.

Dweezel

Technical User
Feb 12, 2004
428
GB
I've got a mysql database that's storing news article information. I've got fields for 'headline' 'date' 'news_source' and 'article'. The article field is set to the 'text' data type.

I don't think this is the ideal way to store this info, but as there will be no more than about 15 rows in the table at any one time, and each article is only about 4 paragraphs long, I think it will be OK.

The problem I'm having is that I've set up a script to edit news items that have already been inserted into the database. After calling the row from the database a html form is written out, and then I've used some php generated javascript to populate the fields with the info from the Database:
Code:
echo "document.onload = document.formname.fieldname.value ='".addslashes($row['article'])."';";
Line breaks are what's causing the problem. For the javascript to work it needs to recieve the text in one long line. I thought nl2br() would sort it out, but it seems that all this does is add a <br> to the text at the same point as the newline rather than replacing it.

Is there anyway that I can get formatted text that contains newlines to output without them?
 
It's ok guys, I've worked it out for myself.
If anyone's interested:

Code:
$textstring = addslashes($row['article']);
$textstring = str_replace("\n","",$textstring);
$textstring = str_replace("\r","",$textstring);
 
why are you using javascript to populate the form (and not just php)?
it would seem better to populate a textfield with the value

Code:
<html>
<body>
This will output properly with line breaks inside a field for editing<br/>
<textarea name="article"><?=$row['article']?></textarea>
<br />
This will output in plain text with line breaks intact <br />
<div style:"width:200px; text-align:left;">
<?=nl2br($row['article'])?>
</div>
<br />
So will this:
<div style:"width:200px; text-align:left;">
<pre><?=$row['article']?></pre>
</div>

</body>
</html>

if you MUST use javascript to do this then i would surprised if it were the newline characters that were causing trouble as they have been escaped in the string (by addslashes) so as far as javascript is aware it is one long string. if this is the problem (and i haven't got my head around it yet) it could that you need to double escape. i.e. once for the echo and once for the javascript. addslashes(addslahes()) - ugly.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top