I want to insert a line Break after every 55 characters i have made a jquery script but it doesn't work can some one please point me in the right direction
Jquery.js
$(document).ready(function() {
$('#text_comment').text($(this).val().replace(/([\s\S]{55})/g, '$1<br />'));
});
Index.php
<input type="text" class="comment" id="text_comment"autocomplete="off"time_date="<?php echo $date_shared;?>"
username="<?php echo $comment_username;?>"post_id="<?php echo $shared_id2; ?>"placeholder="Write a Comment"/>
Line-breaks are automatically removed from inputs with a type of text.
text: A single-line text field; line-breaks are automatically removed from the input value.
You can use a textarea to display the line breaks:
var myString = '1234567890';
myString = myString.replace(/.{1,2}/g, "$&\n"); //replace 2 with 55 for your example
document.getElementById('text').value = myString; // :(
document.getElementById('textarea').value = myString; // :)
<input type="text" id="text"> <-- line-breaks automatically removed
<br>
<textarea id="textarea" rows="7"></textarea>
You can use this approach.
$original = "Sample String.";
$parts = str_split($original, 4); //Replace '4' with '55' afterwards.
$final = implode("<br />", $parts);
Related
Im not familiar with PHP too much and I need to make a function that will read and add some string to every line from my textarea.
Example textarea
AAA
BBB
CCC
And what I need is
somestringAAAotherstring
somestringBBBotherstring
somestringCCCotherstring
and show it as output after sending "post".
EDIT:
My form is
<FORM METHOD ="POST">
<textarea rows="30" name="content"></textarea>
<INPUT TYPE = "Submit" VALUE = "Send">
</FORM>
and yes, question is "how to do it?"
$text = trim($_POST['textareaname']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
foreach ($textAr as $lines) {
$line = $sometext . $lines . $someOtherText;
//proceed furthur
}
I'm trying to create a script which would take an input of formatted text (more specifically a copy and pasted list from a word document, likely bullet pointed or numbered) in a text-box input.
After looking into this I have tried using str_replace and preg_replace but im struggling to get the correct pattern to do what i want here. I'm also unsure on what I can use to 'target' the tabbed space in my pattern. I've tried various ASCII codes with no success (e.g )
Apologies accidentally hit enter while adding tags
Examples of pre-script data:
1. Text
2. Text
3. Text
4. Text
While it doesn't show clearly here, there is a large tabbed space between the numbering and text when pasting.
Post-script:
Text
Text
Text
Text
<?php
$output_space = "";
$output_tab = "";
if(isset($_POST['submit'])) {
$lines = explode('<br />', nl2br($_POST['input']));
foreach($lines as $line){
$tbl_space = explode(" ", $line);
$tbl_tab = explode("\t", $line);
array_shift($tbl_space); // remove first element of the array (everything before the first tab)
array_shift($tbl_tab);
$output_space .= trim(implode(" ", $tbl_space))."\r\n";
$output_tab .= trim(implode("\t", $tbl_tab))."\r\n";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="deformat">
Formatted data <textarea rows="4" cols="50" name="input" form="deformat" placeholder="pasted formatted text here">
</textarea>
Deformatted data (spaces) <textarea rows="4" cols="50" name="output" form="deformat"><?=$output_space?>
</textarea>
Deformatted data (tab)<textarea rows="4" cols="50" name="output" form="deformat"><?=$output_tab?>
</textarea>
<input type="submit" name="submit" value="Clean my data!">
</form>
Assuming you're pasting the content in a TextArea and submitting it, you may well lose the correct tabulation in doing so, so instead use the bullets. My PHP is somewhat rusty, so check the code before you use it, but something along the lines of this will work.
$text = htmlspecialchars($_POST['myFormElement']);
$lines = explode(PHP_EOL, $text);
$bullets = array();
foreach ($lines as $line)
{
preg_match("/([^\s]+)\s+(\w*)/", $line,$captured);
if (count($captured) > 1 ) {
if (strcmp($captured[1], end($bullets))) {
if (count($bullets) > 1 && !strcmp($bullets[count($bullets)-2],$captured[1])) {
echo '</ul>';
array_pop($bullets);
} else {
echo '<ul>';
array_push($bullets, $captured[1]);
}
}
echo "<li>$captured[2]</li>";
}
}
while (count($bullets)) {
echo "</ul>";
array_pop($bullets);
}
Use something like this:
$arSingleLine = explode(PHP_EOL, $yourPostString);
$arRet = array();
foreach ($arSingleLine as $sSingle)
{
$arRet[] = preg_replace('/^.+\s+/', '', $sSingle);
}
// $arRet holds now every single fixed line...
Maybe PHP_EOL does not match your client line ending. For splitting every line ending from a single line of code just use preg_split and the line endings \n \r and \r\n for UNIX, OS X and Windows line breaks.
How can I detect that there is different paragraphs in a form? In this example if the user writes different paragraphs, the echo puts all toguether. I tried white-space:pre and it did not work. I do not know what else can I do to echo the text with <p>?
CSS:
#text {
white-space:pre;
}
HTML:
<form action='normal-html.php' method='post'>
<textarea id="text" name='text' rows='15' cols='60'></textarea> <br/>
<input type='submit' value='Convertir a html' />
</form>
<br />
<?php
$text = $_POST[text];
echo $text;
?>
This sounds like a job for http://php.net/manual/en/function.nl2br.php
string nl2br ( string $string [, bool $is_xhtml = true ] )
Returns string with '<br />' or '<br>' inserted before all
newlines (\r\n, \n\r, \n and \r).
You can use this as you echo out the data, so that way you are never changing what is in the database - or you can simply alter the user input as you save it to the database. Personally I am a fan of the first option, but whatever works best for your application.
Edit: If you want to use only <p> tags, you could also do this using str_replace:
$text = '<p>';
$text.= str_replace('\n', '</p><p>', $_POST[text]);
The \n is generally a new line, depending on how it is read, you may need to use \r\n and the string replace will do the rest. This will leave a spare <p> on the end of the string, but you see where this is going.
You can use the explode function (php manual page):
$your_array = explode("\n", $your_string_from_db);
Example:
$str = "Lorem Ipsum\nAlle jacta est2\nblblbalbalbal";
$arr = explode("\n", $str);
foreach ( $arr as $item){
echo "<p>".$item."</p>";
}
Output:
Lorem Ipsum
Alle jacta est
blblbalbalbal
I have form in html where user can put the text in text area.
I save the content of text area into MySQL database (in field of type TEXT).
Then I somewhere in my aplication I need load that text and put it into array where in each index will be one line of the text.
<textarea rows="10" cols="80">
Row one
Row two
Row tree
</textarea>
array (output in "php pseudocode"):
$array = array();
$array[0] = Row one;
$array[1] = Row two;
$array[2] = Row tree;
How I do it:
I save it to db then I load it and use:
$br = nl2br($string,true);
$array = explode("<br />", $br);
The reason why I use nl2br is I want to avoid problems with end of lines of the text from different platforms. I mean /n or /r/n.
But in my solution must be an error somewhere cause it doesn't work (an array $array is empty).
Better solution would be probably somehow split it into array using explode or something like that with pattern for line breaks but here is again the problem from beginning with line breaks which I don't know how to solve (problems with \n or \r\n).
Can anybody give me an advice? Thanks.
I'd suggest you skip the nl2br until you're ready to actually send the data to the client. To tackle your problem:
// $string = $get->data->somehow();
$array = preg_split('/\n|\r\n/', $string);
When you receive the input in your script normalize the end-of-line characters to PHP_EOL before storing the data in the data base. This tested out correctly for me. It's just one more step in the "filter input" process. You may find other EOL character strings, but these are the most common.
<?php // RAY_temp_user109.php
error_reporting(E_ALL);
if (!empty($_POST['t']))
{
// NORMALIZE THE END-OF-LINE CHARACTERS
$t = str_replace("\r\n", PHP_EOL, $_POST['t']);
$t = str_replace("\r", PHP_EOL, $t);
$t = str_replace("\n", PHP_EOL, $t);
$a = explode(PHP_EOL, $t);
print_r($a);
}
$form = <<<FORM
<form method="post">
<textarea name="t"></textarea>
<input type="submit" />
</form>
FORM;
echo $form;
Explode on PHP_EOL and skip the nol2br() part. You can use var_dump() to see the resulting array.
I am creating a format tool that strips content from articles for print. The demo can be seen here. The full source is avaliable here.
Right now the tool strips formating and can also keep paragraphs by using nl2br What I would like to do is to be able to shift the content to the left and only have a paragraph if there is a break between the content.
for example:
This
is
the
first
paragraph
Second Paragraph
Becomes:
this is the first paragraph
Second Paragraph
I tried this by using regex to check if there were two spaces at the end but that didn't work. Here is some sample code:
HTML:
<form method="post" action="">
<textarea cols="68" rows="21" name="textinput"></textarea><br/>
<input type="checkbox" name="keep_paragraphs" value="true" checked /> Keep Paragraphs<br/>
<input type="checkbox" name="shift_left" value="true" /> Remove whitespace after line unless it ends in two spaces<br/>
<input type="submit" value="Submit" />
</form>
PHP:
$text= $_POST['textinput'];
$p= $_POST['keep_paragraphs'];
$lb= $_POST['shift_left'];
if(get_magic_quotes_gpc()){
$text = stripslashes($text);
// strip off the slashes if they are magically added.
}
$text = htmlentities($text);
//if we should keep formatting
if($p=="true"){
$text =nl2br($text);
}
if($lb=="true"){
$text = preg_replace('/\s+/', ' ', trim($text));
}
echo $text;
Any help on this would be great
EDIT: include example
POST textbox = "Hi Jane
How are You doing today
I hope all is well";
Most text will be coming from e-mails and other sources,basicly it needs to be super genderic.
The regex you need is,
/(?<!\r\n)\r\n(?=\w)/
replace it with a space.
Update
A small correction,
$text ="This
is
a
paragraph
Second Paragraph";
$lb = "true";
if($lb=="true"){
$text2 = preg_replace('/(?<!\r\n)\r\n(?=\w)/', ' ', trim($text));
}
echo $text2;
Your can write this
$text = preg_replace('#\n([a-z])#Us', ' \1', trim($text));