Page 1 of 1

Removing \n and tabs?

Posted: Thu Nov 11, 2004 6:13 pm
by Mr Tech
Hi there!

First, here is my code:

Code: Select all

<?php
$style = ".whitebold{
	font-family:Arial,Helvetica,sans-serif;
	font-size:15px;
	font-weight:bold;
	color:#ffffff;
}
.whitetext{
	font-family:Arial,Helvetica,sans-serif;
	font-size:11px;
	color:#ffffff;
	text-align:justify;
	margin:10px;
	padding:10px;
}
.whitetext2{
	font-family:Arial,Helvetica,sans-serif;
	font-size:11px;
	color:#ffffff;
	text-align:justify;
}
a:link,a:active,a:visited{
	font-size:11px;
	color:blue;
	font-family:Verdana;
	text-decoration:underline
}
a:hover{
	color:blue;
	text-decoration:none;
	font-weight:normal
}
.bottom{
	font-family:Arial,Helvetica,sans-serif;
	font-size:10px;
	color:#3A3839;
}
.bottom:link,.bottom:active,.bottom:visited{
	font-size:10px;
	color:#A51B51;
	font-family:Verdana;
	text-decoration:underline
}
.bottom:hover{
	font-size:10px;
	color:#A51B51;
	text-decoration:none;
	font-weight:normal
}";
$style = str_replace("\n", "", $style);
$style = str_replace(" ", "", $style);
?>
<script language-"javascript blah blah">
_style_sheet = "<?php echo $style; ?>";
etc etc
</script>
<?php
?>
What I want to do is remove all the tabs, spaces and \n's so that the code is all on one line in the _style_sheet field cause javascritp doesn't seem to like it all on seperate lines...

I've tried using the str_replace() function but it didn't work :( The $style is being pulled from a mysql db. No idea why it isn 't working...

Thanks
Ben

Posted: Thu Nov 11, 2004 7:52 pm
by kettle_drum
Try to remove '\n\r' or '\r\n' i forget which way it goes round. If there is still no luck, do a nl2br() on the code and then remove '<br>'.

Posted: Thu Nov 11, 2004 9:15 pm
by mcog_esteban
hi.
try this

$tab = chr(9); //ascii code for TAB(horizontal)
$line_change = char(13).chr(10); //line breaks
$str = str_replace($tab," ",$style);
$str = str_replace($line_change," ",$str);


now you should get a single css line.

Posted: Fri Nov 12, 2004 12:37 am
by rehfeld
ive always used this

Code: Select all

$text = 'your text';

$text = str_replace("\t", '', $text); // gets all tabs
$text = str_replace("\r", '', $text); // gets all CR
$text = str_replace("\n", '', $text); // gets all NL
that will put everything on 1 line

Posted: Fri Nov 12, 2004 8:11 am
by emperor
Alternatively it may be quicker to use an array with [php_man]str_replace[/php_man] like this:

Code: Select all

<?php
    $chars = array (' ', "\t", "\r", "\n");
    $text = str_replace ($chars, '', $text);
?>