Example
$string = "Test Line 1
Test Line 2
Test Line 3";
echo str_replace(PHP_EOL,"/".PHP_EOL, $string );
Actually I am expecting output is
Test Line 1 \
Test Line 2 \
Test Line 3 \
Please help me to solve this problem
Try this,
Edited:
<?php
$string = "Test Line 1
Test Line 2
Test Line 3";
$str = str_replace(PHP_EOL,"\<br />", $string) . " \\";
echo $str;
?>
Use \\ instead of "/"
<?php
$string = "Test Line 1
Test Line 2
Test Line 3";
$var=str_replace(PHP_EOL,"\\"."</br>", $string );
echo $var." \\"; // this append \ in last string
?>
If this is just so that you can put the php value into a JS variable, you can just use json_encode() which will change the new lines to \n for you
$string = "Test Line 1
Test Line 2
Test Line 3";
echo json_encode($string);
// outputs(including quotes) "Test Line 1 \nTest Line 2 \nTest Line 3"
Related
I have a text area in html. Then I fill this taxt area like this
Line 1
Line 2
Line 3
But in post, I get like this :
echo $_POST['textarea'] ;
===> Line 1Line 2 Line3
How can I get like this :
Line 1 <br>
Line 2 <br>
Line 3 <br>
Use nl2br — Inserts HTML line breaks before all newlines in a string
echo nl2br($_POST['textarea']);
How can I wrap <i> </i> tags around This is line one!, This is line two!, This is line three! in $thisStr below, so the result is:
`<i>This is line one!</i>`
`<i>This is line two!</i>`
`<i>This is line three!</i>`
What I did only puts tags around the :: parts. I cant get it to go back and forward and wrap tags.
$thisStr = 'This is line one! :: This is line two! :: This is line three!';
echo preg_replace("/(::)/i", "<i>$1</i>", $thisStr);
This works great, but is not preg_replace:
$thisStr = 'This is line one! :: This is line two! :: This is line three!';
$result= "<i>".str_replace("::","</i><i>",$thisStr)."</i>";
echo $result;
Or, to match your exact example:
$thisStr = 'This is line one! :: This is line two! :: This is line three!';
$result= "'<i>".str_replace(" :: ","</i>'<br/>'<i>",$thisStr)."</i>'";
echo $result;
/*
'This is line one!'
'This is line two!'
'This is line three!'
*/
If you really really really need to use preg_replace this will work
$input_lines = "This is line one!
This is line two!
This is line three!";
$New_string = preg_replace("/This is line .*/", "<i>$0</i>", $input_lines);
The pattern looks for the string "this is line" and the .* means anything in any lenght.
Then the replace pattern is quite easy to understand I guess.
But i recommend the answer Verjas wrote.
There is no need for the extra complexty the preg_replace adds.
Try this:
$thisStr = 'This is line one! :: This is line two! :: This is line three!';
$lines = explode('::', $thisStr);
$result = '';
foreach($lines as $line) {
$result .= '<i>' . $line .'</i>';
}
echo htmlentities($result);
Hope this helps.
I have descriptions stored in mySQL with line breaks, so when I output them as they should be, I use:
<?
echo nl2br($description);
?>
And it shows:
Line 1
Line 2
Line 3
So far, so good. Now, I want to use that same description for the meta tags and the problem is that even if I output them like:
$old_string = nl2br($description);
$new_string = preg_replace("/<br \/>/"," ",$old_string);
echo $new_string;
I still get these meta tags:
<meta property="og:description" content="Line 1
Line 2
Line 3"/>
How do I make it output all in 1 line, with just a space in between them?
Thank you very much for any help :)
Don't use nl2br for META, but simply replace line-breaks on the string from database. Note that depending on your environment, a linebreak can be represented with \r\n or simply with \n. So try to use the following code:
$onelinestring = str_replace("\r\n", " ", $description);
$onelinestring = str_replace("\n", " ", $onelinestring);
Try:
$singleLine = str_replace("\n", " ", $string);
Without nl2br, just the string fetched from the db.
I'm making this chat server, but it doesn't work quite well. When you send a piece of text, it first gets encoded by the function base64_encode() and then gets sent to a MySQL database.
Then the receiver gets the text from that same MySQL database, which is of course first decoded by the function base64_decode().
The only problem is with the special characters like \n \' and \t: when I get the data from the database and print it between two textarea tags, I see \n as a string, and not as actual line breaks.
In short, I need to fix this problem:
$String = 'Line 1 \n Line 2';
print '<textarea>' . $String . '</textarea>';
//The result I want
//<textarea> Line 1
//Line 2 </textarea>
The function nl2br doesn't work, because tags inside a textarea tag won't work, and also because there other characters like apostrophes.
Could anybody help me?
Thanks!
You need to enclose your string into double quotes, for special characters to be evaluated.
$String = "Line 1 \n Line 2";
print '<textarea>' . $String . '</textarea>';
If you change this:
$String = 'Line 1 \n Line 2';
print '<textarea>' . $String . '</textarea>';
to this:
$String = "Line 1 \n Line 2"; // double quote
print '<textarea>' . $String . '</textarea>';
... you will get the output you want.
This one is also works same as using " ... ", however maybe helps in your case:
$string = <<<EOT
Line 1 \n Line 2
EOT;
echo '<textarea>' . $string . '</textarea>';
As the others said, your problem is Single-Quotes.
I know for sure that this was already been asked before but I just googled around and couldn't find anything (maybe wrong word choice?).
Just don't be too mad at me, I'm getting mad too...
I'd like
$string = '
This is a line
This is another line
';
to be shown to the HTML page as
This is a line
This is another line
when I do echo $string;.
How can I capture the return key or the new line and replace it with <br>?
Try the nl2br() function:
echo nl2br($string);
Would return:
<br>
This is a line<br>
This is another line<br>
To trim off the leading and trailing new lines, use trim():
echo nl2br(trim($string));
Would return:
This is a line<br>
This is another line
You can use the PHP function nl2br. It doesn't replace the newlines but, rather, inserts <br /> next to them (which is perfectly fine for your purposes).
Using your example:
$string = '
This is a line
This is another line
';
echo nl2br($string);
/* output
<br />
This is a line<br />
This is another line<br />
*/
Use nl2br function like this:
echo nl2br($string);
If you're not getting it with nl2br, your new line character must not be \n.
print nl2br( str_replace( array( "\r\n", "\r" ), "\n", $string);
Why don't you use:
$string = "
This is a line\n
This is another line
";
?
or use
$string = <<<EOF
This is a line
This is another line
EOF;