Still get <br> in textarea - php

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']);

Related

PHP 8.1 preg_replace only outside of two UBB tags - regex probably wrong

I need to replace the line break \n or \r with the HTML tag <br>, but only outside of [code] UBB tags.
Example string, how it is stored in the DB:
$string = "Test 123
Line break
[code]5: Test
10: With line
15: breaks[/code]
Further writing
Another new line";
Using this code:
$bbextended = array(
"/\[code\](.*?)\[\/code\](*SKIP)(*FAIL)|\\r?\\n/i" => "<br>",
"/\[code\](.*?)\[\/code\]/i" => "<textarea>$1</textarea>",
);
foreach($bbextended as $match=>$replacement){
$string = preg_replace($match, $replacement, $string);
}
echo $string;
I end up with this HTML output:
Line break<br>Test123<br>[code]5: Test<br>10: With line<br>15: breaks[/code]<br>Further writing<br>Another new line
Which means that line breaks within the [code][/code] UBB tags were replaced with <br> (wrong, should not) and the code elements were not replaced by <textarea></textarea> (also wrong).
The expected HTML output is:
Line break<br>Test123<br><textarea>5: Test
10: With line
15: breaks
</textarea><br>Further writing<br>Another new line
I tried to solve it on my own with regex101, but it highlights stuff that shouldnt be highlighted..
I assume there is an issue caused by the actual line breaks and how regex searches.
"/[code[\w\W]*?code](*SKIP)(*FAIL)|\\r?\\n/i" => "<br>",
this seems to work - to detect start and end of my code tags across multiple lines and skip them from formatting with

Regex to find text (Unicode also) that are not surrounded by p tag in an article and give the text surrounding p tag.How to do it?

I have a article which has content on it . I want to find the unwrapped (unicode also present) full text (which are not surround by any tags) and then surround it by p tag and I also dont want to wrap any other tag like iframe,img tag to be surrounded by p tag. Example code are given below-
<p>line 1 </p>
বাংলাদেশ আমার দেশ line 2
<p> line 3 </p>
unwrapped line 4
<p> line 5 </p>
বাংলাদেশ আমার দেশ line 6
<img src="https://unsplash.it/200/300">
<iframe src="https://unsplash.it/200/300"></iframe>
বাংলাদেশ আমার দেশ line 7
From the code above shown I want the output like this-
<p>line 1 </p>
বাংলাদেশ আমার দেশ line 2
<p> line 3 </p>
<p>unwrapped line 4</p>
<p> line 5 </p>
<p>বাংলাদেশ আমার দেশ line 6</p>
<img src="https://unsplash.it/200/300">
<iframe src="https://unsplash.it/200/300"></iframe>
<p>বাংলাদেশ আমার দেশ line 7</p>
As mentioned in comment, "If the reality is as simple as your examples" - then you could simply replace
^(?!<).*$
with
<p>$0</p>
It's even simpler than the one given there. It uses a negative look-ahead to make sure the row doesn't start with a <, and then matches it all. Replacing that with the match (complete one in group 0 - $0) surrounded by the paragraph tag.
See it here at regex101.
A "bit" more complex:
^(?!\s*<[a-z]\w*("[^"\n]*"|'[^'\n]*'|[\w=\s])*>).+$
makes sure it's a "complete tag" starting the line.
See it here at regex101.
Edit
Improved second regex. (Explanation why in the links last paragraph.)
Edit 2
New requirements ;)
^(?!.*<[a-z]\w*("[^"\n]*"|'[^'\n]*'|[\w=\s])*>).+$
This makes sure that the line doesn't contain any tags.
See it here at regex101
You could do something like this if your data is stored on each line:
$file = file_get_contents('test.txt');
$arr = preg_split('/(\n|\r)/', $file);
$string = '';
foreach($arr as $k=>$v){
if (strpos($v, '<p>') === FALSE && mb_detect_encoding($v) === 'UTF-8') { //Per-line: Look for UTF-8 encoding & check if <p> is present
$arr[$k] = '<p>' . trim($v) . '</p>'; //Trim and replace value in array
}
$string .= $arr[$k]; //Add to new string
}
echo $string;
Output:
<p>line 1 </p>
<p>line 2 বাংলাদেশ আমার দেশ</p>
<p> line 3 </p>
line 4 unwrapped
<p> line 5 </p>
<p>line 6 বাংলাদেশ আমার দেশ</p>

How to add symbol in each end of the line in php?

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"

htmlspecialchars is not allowing line breaks?

I have the following apprear exactly like this in a mysql field,
Hello there
world
When i format the above using this code:
echo htmlspecialchars($thestring)
It outputs this,
Hello there<br/><br/>world
How can i get it to do line breaks?
I would like to keep using htmlspecialchars to help with the other html chars.
You need to call nl2br to get the line breaks converted to html:
nl2br(htmlspecialchars($text))
http://php.net/manual/en/function.nl2br.php
<![CDATA[<br/>]]>
this did the trick for me
If you get <br> tags in a string after htmlspecialchars() it means that you have exactly <br> tags in your string (NOT \n line breaks) because htmlspecialchars() doesn't convert \n to <br>.
If so, you have to make a double transformation from <br> to \n and back:
Step 1. Transform <br> to \n
Step 2. Run htmlspecialchars()
Step 3. Transform \n to <br>
Example:
<?php
echo
str_replace( // Step 3
"\n",
"<br />",
htmlspecialchars( // Step 2
str_replace( // Step 1
array("<br />", "<br/>", "<br>"), // make sure that you have your version of <br> tag here
"\n",
$text
),
ENT_QUOTES,
'UTF-8'
)
);
?>

Php line output

I have a file named file.txt that looks like this:
Line 1
Line 2
Line 3
Line 4
using the command:
$content = file_get_contents(file.txt);
echo $content
I get the output on one line:
Line 1 Line 2 Line 3 Line 4
How can I get the output printed over 4 lines?
You actually print it to 4 lines but you can't see in it your browser because of parsing as html.
Use nl2br() to add <br/>'s
$content = file_get_contents(file.txt);
echo nl2br($content);
Also you may send headers that will say that it is not html:
header('Content-type: text/plain');
$content = file_get_contents(file.txt);
echo $content;
<?php echo nl2br($content); ?>
Replaces all regular line breaks ("\n") with "< br >" tags.
If you just want to show a plain text file you should change the content type to text/plain
<?php header("Content-type: text/plain"); ?>
Then all linebreaks will be there as they should in a document.

Categories