Print PHP string in new lines - php

Anything wrong with this code? I want it to print the name and address - each on a separate line, but it all comes up in one line.
Here's the code
<?php
$myname = $_POST['myname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$town = $_POST['town'];
$county = $_POST['county'];
$content = '';
$content .="My name = " .$myname ."\r\n";
$content .="Address1 = " .$address1 ."\n";
$content .="Address2 = " .$address2 ."\n";
$content .="Address3 = " .$address3 ."\n";
$content .="town = " .$town ."\n";
$content .="county = " .$county ."\n";
echo $content;
?>
It looks like the '\n' character is not working.

In your source code this will show on a next line, but if you want to go to another line in HTML you will have to append <br />.
So:
$content .="My name = " .$myname ."<br />\r\n";
I left the \r\n here because it will go to the next line in your source code aswell, which might look nicer if you have to view the source.

The \n character properly works just fine. The problem is, it's not what you expect.
If you see this in a browser, you won't see line breaks, because line breaks are ignored in the source code. The HTML parser only reads <br> as line breaks.
If you try to go to your website and view the source code, you'll find that the line breaks are in there.

Related

PHP. How to remove a %0D character from my code

I am trying to send me an email with a daily digest of some RSS feeds. I receive the following error, caused by an extra %0D character. Could you suggest me what I need to correct in the code to remove it?
Thanks a lot
The error is: simplexml_load_file(https://hdblog.it/feed%0D): failed to open stream: Invalid redirect URL!
// Set the timezone to Rome, Italy
date_default_timezone_set('Europe/Rome');
// Set the cutoff time to 24 hours ago
$cutoff_time = time() - (24 * 60 * 60);
// Set the subject of the email
$subject = "Digest RSS " . date("d/m/Y");
// Initialize the message
$message = "<html><body>";
$message .= "<h1>RSS Digest</h1>";
$message .= "<p>Ecco gli ultimi aggiornamenti dai tuoi feed RSS:</p>";
// Read the list of RSS feeds from a file
$rss_feeds_file = file_get_contents('rss_feeds.txt');
$rss_feeds = explode("\n", $rss_feeds_file);
// Loop through the list of RSS feeds
foreach ($rss_feeds as $rss_feed) {
// Load the RSS feed
$rss = simplexml_load_file($rss_feed);
if ($rss) {
// Initialize a flag to keep track of whether there are feed items to include in the email
$items_included = false;
// Add the feed title to the message
$message .= "<h2>" . $rss->channel->title . "</h2>";
// Add a list of items to the message
$message .= "<ul>";
// Loop through the feed items
foreach($rss->channel->item as $item) {
// Check if the item was published within the last 24 hours
$item_time = strtotime($item->pubDate);
if ($item_time > $cutoff_time) {
// If it was, add the feed item to the email
$message .= "<li><a href='" . $item->link . "'>" . $item->title . "</a><br />";
$message .= strip_tags($item->description) . "</li>";
$items_included = true;
}
}
$message .= "</ul>";
}
}
$message .= "</body></html>";
I am trying to send me an email with a daily digest of some RSS feeds. I receive the following error, caused by an extra %0D character. Could you suggest me what I need to correct in the code to remove it?
Thanks a lot
The error is: simplexml_load_file(https://hdblog.it/feed%0D): failed to open stream: Invalid redirect URL!
Are you running on Windows?
%0D is the carriage return character. You are exploding the input file on \n (line feed), but perhaps your input file has \r\n line terminators. Try exploding on '\r\n' instead.
If your input file has been created on the same platform as you are running your code you could try using the php constant PHP_EOL.

PHP new line character (\n) Not Working [duplicate]

This question already has answers here:
PHP Linefeeds (\n) Not Working
(11 answers)
Closed 6 years ago.
The code is in double quotes and still not working.
I already read this post
I'm using Atom and am on localhost (if that makes any difference)
I redownloaded Atom (in case there was something going on with the settings) and that didn't help
Here's the code:
<?php
$firstName = 'David';
$lastName = "Powers";
$title = '"The Hitchhiker\'s Guide to the Galaxy"';
$author = 'Douglas Adams';
$answer = 42;
$newLines = "\r\n\r\n";
$fullName = "$firstName $lastName |";
$book = "$title by $author";
$message = "Name: $fullName $newLines";
$message .= "Book: $book \r\n\r\n";
$message .= "Answer: $answer";
echo $message;
echo "Line 1\nLine 2";
Output is all one line, but when I view source the new lines are working
Name: David Powers | Book: "The Hitchhiker's Guide to the Galaxy" by Douglas Adams Answer: 42Line 1 Line 2
This is the first thing you will learn if you are learning even from a PHP 5 For Dummies book. HTML doesn't respect new line or tab or multiple space characters. You have to use <br /> for new lines.
* Sourced from PHP 5 For Dummies by Janet Valade.
Change your code to:
<?php
$firstName = 'David';
$lastName = "Powers";
$title = '"The Hitchhiker\'s Guide to the Galaxy"';
$author = 'Douglas Adams';
$answer = 42;
$newLines = "<br /><br />";
$fullName = "$firstName $lastName |";
$book = "$title by $author";
$message = "Name: $fullName $newLines";
$message .= "Book: $book <br /><br />";
$message .= "Answer: $answer";
echo $message;
echo "Line 1<br />Line 2";
If you are just opting for a text based layout, you can set the header to the browser to respect it as just a text file. For that, you need:
header("Content-type: text/plain");
This will render without any HTML.
By default, when a PHP script is run, the content type of the result is set to text/html. When browsers render HTML, newlines are not normally respected, they're treated like any other whitespace.
If you want all your output formatting to stay the same, and you're not sending HTML, tell the browser that you're sending plain text. Put this at the beginning of the code:
header("Content-type: text/plain");
As well Praveen Kumar mention how to print new lines in echo.
But if you still want to use escape sequences then use print_f You can use other escape sequences like \t,\n in printf.
<?php
$firstName = 'David';
$lastName = "Powers";
$title = '"The Hitchhiker\'s Guide to the Galaxy"';
$author = 'Douglas Adams';
$answer = 42;
$newLines = "\r\n\r\n";
$fullName = "$firstName $lastName |";
$book = "$title by $author";
$message = "Name: $fullName $newLines";
$message .= "Book: $book \r\n\r\n";
$message .= "Answer: $answer";
printf ($message);
printf ("Line 1\nLine 2");

Line break and lost swedish letters

I'm building a contact form with PHP that sends the content via mail. I just have a couple of questions that I don't find a solution for on my own.
The first must be simple, but I can't get it why I don't get any line breaks in this code:
$contactForm = $form_name . "\n";
$contactForm .= $form_email . "\n";
$contactForm .= $form_message . "\n";
And my second question is how I can have swedish letters like åäö instead of these strange letters: öäå
I'm using PHPMailer and I have set it to swedish with this line of code,
$mail->setLanguage('se', '/optional/path/to/language/directory/');
EDIT!! I missed to change the path for the language folder in the line above! Have fixed that now!
$mail->setLanguage('se', '/language/');
but it seems like this isn't the problem. I guess it must be some problem with UTF-8 or is it the PHP-code that sanitize the variables?
I'm using this to sanitize the input values:
// Security - call function
$form_name = check_input($_POST['name']);
$form_email = check_input($_POST['email']);
$form_message = check_input($_POST['message']);
// Function to check input
function check_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = strip_tags($data);
return $data;
}
Preciate some help!

PHP function for removing &nbsp and skip a line

I would like to erase &nbsp in Wordpress post when we skip a line but keeping the "skip a line".
In post.php I've added this function :
function remove_empty_lines( $content ){
$content = preg_replace("/ /", "\n", $content);
return $content;
}
add_action('content_save_pre', 'remove_empty_lines');
but the \n doesn't work, what can I write for that works ? (<br /> doesn't work too).
\n doesn't represent a new line in HTML, so you won't see the line break when you echo the result. Either use "<br />" directly as the replacement, or use the default nl2br() PHP function to insert HTML line breaks before PHP line breaks e.g.
$sample = "testing a \n line break";
echo $sample;
// HTML output is:
"testing a line break"
$sample2 = "testing a <br /> line break";
echo $sample2;
// HTML output is:
"testing a
line break"
$sample3 = "testing a \n line break";
$sample3 = nl2br($sample3);
echo $sample3;
// HTML output is:
"testing a
line break"
Newlines in HTML are expressed through <br > or <br/>, not through \n.
$content = str_replace(" ", '<br/>', $content);
echo nl2br($content);

Syntax error: Unexpected '/' error

I'm using fopen() but I get this error on execution.
Parse error: syntax error, unexpected '/' in /home/furtherpath.. on line 7
The line 7 is:
/home/a11*****/public_html/rishi/rishi_someone_php.php = fopen("rishi_someone_php.php","r");
I know I should place a file handler instead but that also doesn't seem to work and gives the same error.
Can't figure out why.
What I'm doing is creating a html page using php.:
$html="<html> \n <head> \n <title>".$fn."</title> \n </head> \n <body> \n <?php \n $file=fopen('".$full."','r'); \n while(!(feof($file))) \n { \n echo htmlspecialchars(fgets($file)).'<br/>'; \n } \n fclose($file); \n ?> \n </body> \n </html>";
Thanks in advance.
Here is a cleaned up version of your script. Try to understand it. I'e combined the string at each html element just to be clear. There is no reason for php tags within php tags. In your code at this line $file=fopen('".$full."','r');
You are trying to quote a variable of type string. That is not necessary with variables. PHP will know that it is a string, so don't do that. Only use quotes when a string literal is being passed into the function as an arguments.
$html = "<html>\n<head><title>". $fn."</title>\n</head><body>\n";
$fp = fopen($full, "r");
if ($fp) {
while (($line = fgets($fp)) !== false) {
$html .= htmlspecialchars($line, ENT_QUOTES, 'UTF-8');
$html .= "<br />";
}
} else {
$html .= "No data";
$html .= "<br />";
}
fclose($fp);
$html .= "</body>\n</html>\n";
echo $html;

Categories