Formatting line breaks in a <textarea> - php

I am trying to PHP data that is posted from one place to another textarea.
Here is the case:
after doSearchTweet.php shows the tweets that are retrieved using twitter API, user can checked the tweets they want using a form.
The form will post the selected tweets a textarea input.
Here is the problem, I cannot style the selected tweets in the textarea.
For example: Username - Jakob Tweet - I need help in styling php!
I select this tweet and the data is post into the textarea, it shows this
JakobI need help in styling php!
instead of
Jakob
I need help in styling php!
I tried using normal html tags like <br/> n/ in the textarea, instead of applying the html tags, it will appear as normal text like this
Jakob< b r >I need help in styling php!
here is my code
<tr><td><label for="content"><span class="postBigFont">Content: </span></label></td>
<td colspan="4"><textarea cols="60" rows="15" name="content">
<?php
$user = $_POST['username'];
$content = $_POST['content'];
$add = $_POST['add'];
foreach ($add as $i){
echo $user[$i];
echo $content[$i];
}?>
</textarea></td></tr>

Replace "<br>" with "\n" in your textarea output.

Perhaps nl2br($text); is the answer

$content = str_replace("\r\n", "<br />", $content);

Related

HTML textarea transferred to echo in PHP with line breaks as per user input

My textarea in HTML:
Description<br><textarea name="description" rows="5" cols="27"></textarea>
Within my PHP file I have:
$desc = $_POST['description'];
echo $desc . "<br>"; // needs line breaks as per user input in textarea.
The <br> I have used within my echo is simply there to place some room before and after the other content being entered when the form is submitted.
The text entered into the description box will show up when the form is submitted, however it is on one line and not broken up as shown in the textarea when the enter key is pressed.
How can I include the breaks from the description box into the PHP echo?
EDIT:
I used...
echo "<pre>" . $desc . "<br>";
within my PHP file, works well. Thank you!
Try Using:
echo $desc = nl2br($_POST['description']) . "<br>";
The manual for nl2br. Can be found in the link below
http://php.net/manual/en/function.nl2br.php
You can place the text inside a <pre> element, or any element with the CSS attribute:
white-space: pre;
And the white space, as well as the raw text, will be preserved (i.e. you don't need to modify the value you have been supplied).
Just be careful what you allow to be injected into your HTML, as all user input is unsafe and provides an opportunity for a number of HTML injection attacks.
.output {
white-space: pre;
}
<div class="output">This is sample output.
And it has line breaks.
So it is three lines.</div>
echo $desc = nl2br($_POST['description']) . "<br>";
is a simple, one line solution :)

Style text with HTML tags

I have a string saved in a database as <b>hello</b>
When I get the string from the database using a query, the text isn't bold (which should be caused by the <b> tags). Instead, it simply displays as 'hello'.
How can I apply the html tags to the text ?
<?php
$stmt = $con->prepare("SELECT * FROM posts");
$stmt->execute();
$text = $row['text'];
echo $text;
?>
I have tried using htmlentities as well as html_entity_decode, but the result is the same.
I'm unable to use html tags in the output ($text = "<b>" . $row['text'] . "</b>";) as I'm getting multiple strings from the database, each with different html tags.
#Fred-ii- Hi Fred, just to let you know that I tried htmlspecialchars_decode(stripslashes($row['text'])) again and out of the blue, it strangely worked. – The Codese
As stated in comments:
htmlspecialchars_decode(stripslashes($row['text']))
is what should have been used.

How to get a url from a database

So I have three pages one that is the index page. One that writes the data from a form inside the index page to the database. And one that gets data from the database and echos out a html table with the data inside.
Currently if you write a link in the form. It will just come out as text. I would like the whole link to be like [link].
so say if I wrote this onto the form:
Look at this: www.google.com or Look at this: https://www.google.com
it would come out like this in html
Look at this: www.google.com
How could I go about doing this?
Okay so the html is:
<form class="wide" action="Write-to.php" method="post">
<input class="wide" autocomplete="off" name="txt" type="text" id="usermsg" style="font-size:2.4vw;" value="" />
</form>
in which the user would write:
"Look at this: www.google.com or Look at this: https://www.google.com"
This would then get sent to the database through Write-to.php.
$sql="INSERT INTO social (comunicate)
VALUES
('$_POST[txt]')";
}
this then gets written back into the database:
$result = mysqli_query($con,"(select * from social order by id desc limit {$limit_amt}) order by id asc");
while($row = mysqli_fetch_array($result))
{
echo "<tr div id='".$i."' class='border_bottom'>";
echo "<th scope='col'>";
echo "<span class='text'>".htmlspecialchars($row['comunicate'])."</span><br />";
echo "</th>";
echo "</tr>";
}
Just try:
echo(''.$your_url_variable.'');
Update:
The OP really wanted to detect url's in a string. One possible solution could be filter it using a regular expression. This code could help:
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.com";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "{$url[0]} ", $text);
} else {
// if no urls in the text just return the text
echo $text;
}
?>
Source: http://css-tricks.com/snippets/php/find-urls-in-text-make-links/
There are quite a few things you need to worry about when displaying user supplied (tainted) data.
You must ensure that all the data is sanitised -- never ever just echo the content, look into htmspecialchars and FILTER_VALIDATE_URL for example:
function validateUrl($url) {
return filter_var($url, FILTER_VALIDATE_URL);
}
What you are attempting to do is convert a string into a link, for example you can write a function like this:
function makeClickable($link) {
$link = htmlspecialchars($link);
return sprintf('%s', $link, $link);
}
You can use string concatenation as well, but I wouldn't do that in my view code. Just personal preference.
Take a look at the urlencode function, it will certainly come in handy.
I would also recommend you read about cross site scripting
Please note that I am not making any implementation recommendations, my aim is just to show you some contrived code samples that demonstrate making a string "clickable".
Update:
If you would like to make links clickable within text, refer to the following questions:
Best way to make links clickable in block of text
Replace URLs in text with HTML links
save the hyperlink in db and retrieve as a string by sql query
like:
select link from table_name where index = i
and save link as: whaatever here
and print it
Use this
echo '' . $res['url'] . '';

Outputting correctly a very long textarea value in PHP

I have a textarea with input like this:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
I want an output with line breaks like this:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
How can I do that?
In my previous question, someone suggested wordwrap, which would be useful if the line had spaces.
Yes, use wordwrap...
$text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
$newtext = wordwrap($text, 10, "<br />", true);
echo $newtext;
I don't know if I understand the question correctly, but HTML textarea field also has the wrap attribute (values "soft", "hard", or "off"). Hard wraps the words inside the text box and places line breaks at the end of each line so that when the form is submitted it appears exactly as it does in the text box.
So:
<textarea cols="30" rows="5" wrap="hard">text..</textarea>
you could insert a "<br /> at the index, where the break should occur - only for the output.
textbox.value = HandleLineBreaks(line, index);

strip <br>(n12br) get from database field

i have a textarea value which its value derived from a field(nl2br)
how to strip off "< br/>", so that when i want to edit this field, the "< br />" will not be appeared?
//$data["Content"] is the field that has <br/> tags inside
$content = $data["Content"];
//when want to edit, want to strip the <br/> tag
<td><textarea name="content" rows="10" style="width:300px;"><?=$content?></textarea></td>
i know it should be using strip_tags() function but not sure the real way to do it
any help would be appreciated
If you wanna use strip_tags, then it would just be:
$content = strip_tags($data["Content"]);
i would be using str_replace the following will replace <br/> with newline
$content = str_replace('<br/>','\n',$data['Content']);
or if you don't want the newline
$content = str_replace('<br/>','',$data['Content']);
edit
an example
$my_br = 'hello<br/> world';
$content = str_replace('<br/>','',$my_br);
echo $content;
Output: hello world

Categories