PDA

View Full Version : Formatting HTML code in a PHP script


Ge64
12-18-2006, 08:12 AM
I want to use this code to add a \ to the end of every line in a peice of HTML code:

<?php
if (isset($_POST['input'])) {

$input = $_POST['input'];

$var = explode('\n', $input);

for ($i = 0; $i <= (count($var)-1); $i++) {
$var[$i] .= '\';
}

$output = implode('\n', $var);
}

?>

When I run it (even without an input) i get this:

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\Webserver Root\forum\formatscript.php on line 12

Parse error: parse error, unexpected T_STRING in C:\Webserver Root\forum\formatscript.php on line 12

koyama
12-18-2006, 08:18 AM
In php the backslash is the escape character in strings. The backslash itself is represented as '\\'

You want instead

$var[$i] .= '\\';

Ge64
12-18-2006, 08:21 AM
In php the backslash is the escape character in strings. The backslash itself is represented as '\\'

You want instead

$var[$i] .= '\\';


Thanks, no more errors, except the script doesn't work yet:

It only adds a backslash to the end of the entire input. So the explode doesn't work, how would I split a string by lines?

Edit: Nevermind this thread I dont need it due to another solution

koyama
12-18-2006, 08:48 AM
'\n' is not the newline character, but "\n" is

What you want is

$var = explode("\n", $input);