I need to format a bunch of HTML into 1 line. Example <TBODY> <TR> <TH>Poids </TH> <TD>9 KG/UN </TD></TR></TBODY> HTML: Now when I run the following code it works for normal characters but it doesn't work with HTML tags. <?php $string = preg_replace( "{\s+}", ' ', $string ); echo $string; ?> PHP: Desired output would be. <TBODY><TR><TH>Poids </TH><TD>9 KG/UN </TD></TR></TBODY> HTML:
Try this <?php $str = "<TBODY> <TR> <TH>Poids </TH> <TD>9 KG/UN </TD></TR></TBODY>"; $str = preg_replace_callback('~\<[^>]+\>.*\</[^>]+\>~ms','stripNewLines', $str); function stripNewLines($match) { return str_replace(array("\r", "\n"), '', $match[0]); } echo nl2br($str); ?> Code (markup):
Thanks, but that removed the HTML tags too. I just want to make the HTML all one line so it can go in a CSV file. Output would look like...
Here you go; no need to use regular expressions: <?php $html = <<<HTML <TBODY> <TR> <TH>Poids </TH> <TD>9 KG/UN </TD></TR></TBODY> HTML; $html = str_replace("\n","", $html); highlight_string($html); ?> PHP:
Thanks guess highlight string is what I was missing. Still got this stray "1" character though. Makes no sense on where it is coming from. http://www.ubottutorials.com/newline.php <form id="form1" name="form1" method="post" action="newline.php"> <label>Input :</label> <textarea name="input2" id="input2" cols="45" rows="5"></textarea> <br /> <input type="submit" name="submit" id="submit" value="Submit" /> </p> </form> <?php $string = $_POST["input2"]; $string = preg_replace( "{\s+}", ' ', $string ); echo highlight_string($string); ?> PHP:
oh ic u wanted the output on the browser to be like that i posted it for source code i guess u got the code from the above guy now
Aaron, you don't use echo() with highlight_string(), remove the echo() and use the following: <form id="form1" name="form1" method="post" action="newline.php"> <label>Input :</label> <textarea name="input2" id="input2" cols="45" rows="5"></textarea> <br /> <input type="submit" name="submit" id="submit" value="Submit" /> </p> </form> <?php $string = $_POST["input2"]; $string = preg_replace( "{\s+}", ' ', $string ); highlight_string($string); ?> PHP:
Sure; no problem. You might also want to add (if you havent already). An if statement to verify if the forms been submitted, that way you can contain the printed code to only print when the form is submitted. <form id="form1" name="form1" method="post" action="newline.php"> <label>Input :</label> <textarea name="input2" id="input2" cols="45" rows="5"></textarea> <br /> <input type="submit" name="submit" id="submit" value="Submit" /> </p> </form> <?php if(isset($_POST['submit'])) { $string = $_POST["input2"]; $string = preg_replace( "{\s+}", ' ', $string ); highlight_string($string); } ?> PHP: