开发者

Storing line break in a php Variable

开发者 https://www.devze.com 2023-04-05 10:25 出处:网络
I am developing a Facebook App in which I get some text from the Database and Display it on the Page.

I am developing a Facebook App in which I get some text from the Database and Display it on the Page. The Problem is that I want to insert a line Break in the variable e-g If I copy a Text from database and store it in a Variable.. Let say $text="I love to walk";

I want to insert a line break after "to" how can i do that?

I had tried to store the text like this in html 
"I love to <html> <br> </html> but that didn't worked..

Just suppose this is the Text ..may be next time the te开发者_开发问答xt is entirely Differnet having no "to" word.


Depends on if you want to create new line in code output, or in HTML

$nl = "\r\n";
$nl_html = "<br />";

That exmple you provided modify like this:

$lyrics = "I love to <br> but that didn't worked.."

To automatically add line break after some text, use preg_replace

$lyrics = preg_replace('/to /',"to<br />",$lyrics);

see http://php.net/manual/en/function.preg-replace.php


$new_str = str_replace('to', 'to <br />', $str, 1);


If you want to output the text in a html page, you need to make it

$text="I love to <br /> walk";

If you want to output it to a file you need to make it

$text="I love to\r\nwalk";
or
$text="I love to\rwalk"; depending on the OS on which you will be reading the file


Hi I was having issues with this just as you do earlier today (I am new to PHP).

The way I fixed this was as follows: $format_text = nl2br($formatthis); You would then refer to $format_text.

What it does is it keeps the line breaks.

However I am not quite sure what you mean with your OP, after re-reading it. I went by the topic and I answered it as best I could.

If you are having trouble let's say echoing html code then most definitely you are having trouble with escaping characters. For instance: echo "a href="something" /a this won't work.

echo "a href=\"something\" /a this will work, notice the .


you have two options either use preg_replace or use a variable to save the value please see php documentation for further info

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号