I have wrote a simple php script which reads an IMAP email account and displays the body of the most recent mail. There is just one problem, it won't keep the new lines properly. It just puts it all on one line.
I use
imap_fetchbody($conn, $latest, "1");
to read the body of the email. How do I keep the original formatting with all the proper line breaks. Much thanks
Are you outputting to a browser? Try using nl2br. The doc says:
Inserts HTML line breaks before all
newlines in a string
Example:
<?php
echo nl2br("foo isn't\n bar");
//output: foo isn't<br /> bar
?>
Why don't you try the print_r option which is in built in PHP?
Related
I call for example this element SEND_EXTRA_ORDER_EMAILS_TO form my database
SEND_EXTRA_ORDER_EMAILS_TO = test<test#test.com>
when I write in my php code an echo, print_r or var_dump
eg
var_dump(SEND_EXTRA_ORDER_EMAILS_TO) it write only test.
do you have an idea how to resolve this element be cause it's make to send an email
I use php 7 and mysql 7
Tk
You can use htmlspecialchars() to escape the string so your browser wont treat your string as HTML.
$code = "<h1>Hello world</h1>";
echo htmlspecialchars($code);
// Will output <h1>Hello world</h1> without rendering the HTML in the browser
try with that htmlentities(EMAIL_FROM) and works
I'm trying to create a block of PHP code within HTML such that when the user loads the page, it displays their IP address and time/date as the user in an email address.
I'm using apache on fedora21, and have enabled PHP (tested with phpinfo() function in the same HTML file).
Here is the code I'm trying to execute:
<? echo '<a href="mailto:'.$REMOTE_ADDR.'_'.date('y-m-j').'-#example.com" title="There is no spoon">For stupid spambots'; ?>
It just prints For stupid spambots'; ?> without printing the generated email address.
<? echo 'For stupid spambots'; ?>
Need to close <a href at first, and if you want to return IP its $_SERVER['REMOTE_ADDR'] not $REMOTE_ADDR except you define that variable before.
Judging from the fact that you can see closing ?>, I deduce that your PHP code doesn't run at all and is interpreted like regular HTML.
There might be several reasons why (badly configured Apache being one of them), but my prime suspect is that you have disabled short PHP tags. Try using <?php instead of <?.
You used the syntax of an html anchor wrong. Consider this:
<?php
$address = sprintf('%s_%s-#example.com', $_SERVER['REMOTE_ADDR'], date('y-m-j'));
echo sprintf('%3$s: %1$s',
$address,
'There is no spoon',
'For stupid spambots');
?>
You have to print the address into the visible text content of the anchor definition if you want it to be visible. You only but the "For stupid spambots" string in there which is what got displayed.
( Note that I just used the sprintf() calls to keep the lines short and readable. Obviously this also works with traditional string concatenation. )
I'm relatively new to this and am familiar with echo in PHP but what I need is to have the contents of a field in a database to be placed in the page (but not as a written field)
For example
<?php echo $product_description['description']; ?>
the field description has html formating in it already so when I use 'echo' it writes out for example
<p>text on firstline <br> text on next line
And what I want is that this html from this field in the database that already has html formating to simply placed in the php page which would make it look like this
text on firstline text on next line
I assume I just need to use a different command than ECHO but don't know which one.
Try
echo html_entity_decode($product_description["description"]);
If that works, the HTML in your database has been encoded using htmlentities, so you must decode it to write to a page.
strip_tags is what you're after.
Use it like so: echo strip_tags($your database bit to echo here);
PHP docs for strip_tags
Very simply, i want to make a variable reads the html code as string ,, i mean dont execute it (run it) .
the problem with the code is : i have a html file , and i want to get the content of it , and make some preg_replace for it (run a function on the html code), the problem is i cant use preg_replace, or any another function because the html code is executed by php (php reads the html code)..
i wish you understand me, i want something like highlight_string, but it save the html code in the variable.
Thank you.
you're probably trying to include or require the HTML code.
which is incorrect since it is evaluated as part of the source.
instead, use a function such as file_get_contents() to read the file into a string.
Use file_get_contents() as #David Chan suggested and then pass the result through htmlentities()... it converts the characters to HTML entities (i.e., < to <).
$getTheContent = file_get_contents($filepath);
echo htmlentities($getTheContent);
It should return the code, not executed.
so I need the user to write a review about an article or book and send it to a DB via PHP but with some basic HTML formatting.. I mean, I have a form , when the user writes the review, the data is sent but without any kind of formatting, If the user want to write in a new line, the text is sent like plain text, I need to get also those new line breaks and simple stuff.
I know how to use PHP and DB connection, I just need to know how to get those new line breakes and stuff..
Use nl2br
Just before printing on the screen data from DB. It replaces \n (new line) as <br>
I recommend storing the data as plaintext, and adding the formatting on the way out. This way if you want to change the way it is formatted then you don't have to update every row in the database.
you can use nl2br() if you just need to newlines to be formatted, and a search-and-replace for anything else.
Have you considered using an existing 'plain text to markup' solution, like Markdown?
It (and others like it) allow your users to write plaintext reviews that will be sensibly formatted. (like stackoverflow uses!)
The PHP function nl2br() basically takes every new line your user enters via the form and converts the new line code to a <br> tag.
An example of using this would be:
$text = nl2br("This is text \nThis is a new line of text");
This would create the following code in your database:
This is text<br>This is a new line of text
When the user hits enter in the form textarea, PHP will pick this up as \n.