Converting \n in C# to a new line in PHP - php

I am re-wrting this agian as people dont seem to understand what i want.
I am presenting a table with information from my SQL database. When i insert into my SQL database i add
\n
so when i 'draw' the database i can just replace it with
<br>
and it will add a new line. Heres what i tired and it doesnt seem to work:
But that doesnt seem to work, it wont replace the strings, but if i replace '$row['Info']' with something like "Hello \n Test" it will print hello and Test onto seperate lines.

In your string replacement function, replace
"\n"
With
"\\n"
As the escaping cancels the first one out.

Related

Is there a typo in this str_replace code? / Am I reading it correctly?

Here is the line of code from a PHP file, specifically it is from zstore.php which is a file include as part of the "Zazzle Store Builder" toolset from Zazzle.com
The set of files allows someone like me, who has products for sale on Zazzle and massage that data into a nicer "storefront" which I can set up my way instead of being confined by the CMS structure of Zazzle.com where they understandably want to keep the monkeys (uhmmm... users like myself) from causing too much mayhem.
So... here is the code:
$keywords = str_replace(" ",",",str_replace(",","",$keywords));
Two questions:
Am I understanding what it does and
Is there an extra single or double quote in the string that does not need to be there?
Here is what I think the line of code is saying:
Take the string of characters that the user inputs (dance diva) and assign it to the variable called
$keywords
then run the following function on that character string
= str_replace
(" ","," <<< look for spaces. If you find a space, replace it with a comma
,str_replace(",","" <<< this is the bit I don't understand or which may have a typo
I THINK that it is saying " if you find commas, leave them alone, but I'm not certain.
,$keywords)); <<< then put the edited string of characters backing to the variable called $keywords.
What lead me to look at this was that I was inputting the following:
dance,diva which is what I THOUGHT the script was wanting from me based on the commented text in the README.txt file:
// Search terms. Comma separated keywords you can use to select products for your store
So..
Am I understanding what this line of code is supposed to do?
which, assuming I am correct, and I'm pretty sure that the first half is supposed to work as I've described, now brings me to my second question:
Why isn't the second bit working? Is there a typo?
To review:
dance diva produces results
dance,diva does not
Both, SHOULD work.
Thanks in advance for your help. I have a lot of HTML experience and computer experience but PHP is new to me.
$keywords = str_replace(" ",",",str_replace(",","",$keywords));
You can split into
$temp = str_replace(",","",$keywords);
$keywords = str_replace(" ",",",$temp);
First it replaces all comas with empty string, it is removes all comas. Then replaces all spaces with comas.
For "dance diva" there are no comas so first does nothing, then it replaces space and result is "dance,diva"
For "dance,diva" it removes coma, you get "dancediva" and there in no space to replace next so it is Your result.

how to get userinput formatted text from mysql

I used textarea to upload the user discription into the database of phpmyadmin. User can use any characters and spaces, new line, and other special characters. When I retrive the infomation from the database its showing all in one para with no formatted text. I want the text to appear the same as the user inserted like how its appearing in the forums. Below are the codes I used....
Input html
<textarea class="noticearea" name="notice_area" id="notice_area" required="required"><?php echo htmlspecialchars($_POST["notice_area"]); ?></textarea>
php read
$notice_area = mysql_real_escape_string($_POST["notice_area"]);
SQL insert
$noticeinsert = mysql_query("INSERT INTO notice (notice_area) VALUES ('$notice_area')");
retrive infomration
<?php echo htmlspecialchars($notice_area); ?>
Everything is fine no error or warning. I can retrive easily but not in a format. Please suggest me if i have to add any code.
You might need the following code for displaying.
// $notice_area assumes you have database query and the reuslt is correctly fetched.
echo str_replace("\\r\\n","<br/>", (htmlspecialchars(($notice_area))));
Why?
http://php.net/manual/en/function.mysql-real-escape-string.php
mysql_real_escape_string() will add slashes prior to \n , \r, so on.
after inserting into db then are then \n \r in plain text because you added them by \\n and \\r.
so after getting the plain text of \n and \r, you need to replace them with , then it is it :D

Do I really need to add "\n" to every PHP statement that I write in a script if I dont want my output to be like totally messed up?

I'm learning PHP and writing it and executing in the browser is cumbersome.
So I write it as a script and execute it on the terminal, such as
me#machine $ php script.php
However, it seems to me, all statements are printed to the same line, if not explicitly a newline character is also printed.
<?php
echo "Hello World.\n";
?>
If I omit \n, I end up with
me#machine $ php hello_world.php
Hello World. > me#machine $
which kind of is lame.
Do I really, like really really (as in "totally really"), need to type \n for every statement I like to test?
You've got a choice:
Include a \n on the end, and have a line feed.
Leave out the \n and don't have a line feed.
It's up to you. No, you don't need to have it there, but if you want to output text to the command line, you probably do want it.
I guess there's one other alternative. Since PHP outputs content that is outside of the <?php .. ?> tags as plain text, you could just put a blank line at the end of your code after the final ?>. That will cause PHP to output a new line at the end without you needing to write \n.
But to be honest, putting the \n in your string is better coding practice. (And frankly, \n isn't exactly the worst thing in the world to having in your code. if you can't cope with the horrors of seeing \n in your code, then you're going to have a hard time reading most program code anyway... just wait to you learn Regex!!!)
No. You might write your own writeLn() function and maybe use PHP_EOL instead of \n, depending on what the script is for. (New-line string differ across systems, and PHP_EOL is your server's version of new-line, so it makes your script portable at least in regard of the running environment.)
Yes, you need to use \n to print a new line.
PHP will only print that which you tell it to print. So, if you want a new line then you need to print a new line.
Depending on your requirements.
\n is representing a new line break.
So if you are testing a single command with a single echo, from command line you might beable to go without the newline break.
If you have multiple echos, all the output will be jumbled into one long text.
function NewLine ($Text)
{
$Text = $Text."\n";
return $Text;
}
echo NewLine('This Text Will Have A New Line Appended To The End');

SQL Insert Into with HTML <p> tag

I have a text field that inserts its content into an SQL table. Often, I will want this content to have <p> html tags within, based on line breaks in the text field. I have tried doing a replace before inserting:
str_replace("</p><p>", "\n", $_POST["body"]);
and I have tried doing a replace with escape characters:
str_replace("</p><p>", "\n", $_POST["body"]);
with no success. Meaning they still appear as text field line breaks. There is no security issue as the field can only be accessed by an administrator. Thank you for your help.
it seems both times you are trying to replace contrary - a </p><p> to \n which obviously fails
try to swap str_replace argumants.
I don't understand what SQL has to do here though
How about replacing each separately?
str_replace(array("</p>", "<p>"), array("\n","\n"), $_POST["body"]);
I think the problem is in " latter, like google, try replacing it with ' or call function addslashes() on your text.

How can I get a new line in a textarea?

I have a textarea that I need to put a new line into with some dashes above. I have tried nl2br but that just echos the <br> tag. I have also tried to concat the \n but it is ignored.
What this is for is an email like system. When the user replies to an email, I want the old message below with a seperator like a few dashes. I can't get this to work though.
new message starts here
--------------
old message here
Can someone please give a hand?
Thanks.
Try a return character: \r
According to this article, the \n newline character should work. What is happening when you are inserting the \n character into the string using double quotes?
This works for me:
<textarea>test
testing</textarea>
it properly creates the next line. Check to make sure your source code looks something like that, with n actual line break in the source where you want it to be.

Categories