automatic line-break of textarea inside a form possible with PHP? - php

I know about the nl2br function... But is there a way to set a maximum length of a line?
Im using php to get results from a form, which contains a textarea.
I dont want words to get broken to newlines, like the word 'example' to become 'example'
Thanks

You can use wordwrap to force a line break:
wordwrap($str, 123, "<br>\n", true)

You might be interested in the wordwrap function in php.

Related

Newline inside variable string

What if I have a long input of string? maybe 50-250 characters.
and I echoed the variable it would echo a very long line of letters and words.
So, how will I let the variable auto Newline itself? maybe after 50 characters it would new line when echoed.
I have thought of counting the length of the string and looping it and after counting 50 characters it would Newline but if i do that it would slow down the Site.
is there any function to Newline a VARIABLE STRING? or i should use Looping instead?
You can use a built-in PHP function: wordwrap.
If you want to cut at 50 characters and insert a HTML line-break:
wordrap($str, 50, "<br />");
You can also use the last parameter to force return, even if the string does not contain spaces.
You can insert escape characters into a string, such as \n which will be translated to a new line break.
You can use PHP_EOL in conjunction with strlen() to acheive what you want.
Just adding more info about \n:
// PHP 5.5.12
// This works for outputing to browser:
$XmlHeader = '<?mso-application progid="Excel.Sheet"?>' . "\n";
// These do NOT work:
$XmlHeader = '<?mso-application progid="Excel.Sheet"?>' . '\n';
$XmlHeader = '<?mso-application progid="Excel.Sheet"?>\n';
If you can manage the string in php i think the definitive solution is using function wordwrap: http://www.php.net/manual/en/function.wordwrap.php
You can set the max lenght of a row, the braek parameter (in your case /n).
The function returns a string with the /n breaks and you can print it.

What's throwing off my str_word_count?

I'm using PHP's function to count the number of words from a textarea via POST...
The issue is that if I do a post back to my file and output the word count it is different than if I copy and paste the same text into my PHP script to evaluate the word count.
What is throwing off the number? There is difference of 6 words, incidentally there are 6 double line breaks in the textarea as well.
How do I minimize this difference?
You could remove the line breaks and tags altogether:
str_word_count(str_replace('<br>', '', nl2br(strip_tags($data))));
Or I guess this is better:
str_word_count(strip_tags(nl2br($data)));
If your line breaks are in HTML-form, you could use something like strip_tags()
If they aren't, I suspect an issue with encoding. Maybe an combination of stripslashes, utf8_encode or utf8_decode could solve this wrong counted words.
As an last resort you could use some regular expression to filter anything but [a-zA-Z] and spaces.

Use line-break as separator for an array input?

I've never actually used arrays before, as I've never had to so far (a simple variable has been enough for me), however now I've created a form with a text-area that is meant to POST multiple urls through to my PHP script.
What I want to do is use a line-break in the visitors input to act as a separator for an array input.
For example, the visitor inputs 90 lines of text (all url's), the array breaks each one into a list of 90, and creates an array value for each one.
Any info, advice or comments would be greatly appreciated :)!
Not 100% percent sure what line breaks are used, e.g.:
Windows uses \r\n
Linux uses \n
(old) Macs used \r
However if you know this you can simply do:
$urls = explode("\n", $_POST['urls']);
EDIT
Actually after testing using regex IS faster than first doing a str_replace() and explode.
Look at http://www.php.net/manual/en/function.preg-split.php and as delimiter use new line sign
or see PHP REGEX - text to array by preg_split at line break
be careful about using just \r or \n because every operating system has "new line" defined another way
see answer by Tgr on SO question PHP REGEX - text to array by preg_split at line break
Use explode
$array=explode("\n",$_POST['textarea']);

Limit amount of br's nl2br shows in a row?

I'm having some problems with a "bb parser" I'm coding. Or, well, not with the parser itself, but the nl2br modifying it.
The string from the database is like the following:
text text text
[code]code code code[/code]
text text text
Now, nl2br puts one br / after the first "text text text", and then another one below that, so there's two line breaks before the [code] tag (which actually is correct, but not what I want).
Is there any way I can limit how many br's are entered in a row? I can't seem to find a solution that's simple enough.
Thanks in advance, guys.
In addition to previous solution, I add a different one, since Fredrik asked for it. This will replace double <br> after nl2br instead of before.
$string = nl2br( $string );
$string = preg_replace( '/(<br(?: \\/)?>\\r?\\n?\\r?)(?=\\1)/is', '', $string );
You could for example replace two linebreaks (or more) by one by using preg_replace :-)
You could use
$string = str_replace(array("\r\n\r\n", "\n\r\n\r", "\n\n", "\r\r"), array("\r\n","\n\r","\n","\r"), $string);
This prevents double <br> tags. Preg_replace as suggested before is better if there could be more than two new lines in a row.

PHP: How to prevent unwanted line breaks

I'm using PHP to create some basic HTML. The tags are always the same, but the actual links/titles correspond to PHP variables:
$string = '<p style="..."><strong><i>'.$title[$i].'</i></strong>
<br>';
echo $string;
fwrite($outfile, $string);
The resultant html, both as echoed (when I view the page source) and in the simple txt file I'm writing to, reads as follows:
<p style="..."><a href="http://www.example.com
"><strong><i>Example Title
</i></strong></a></p>
<br>
While this works, it's not exactly what I want. It looks like PHP is adding a line break every time I interrupt the string to insert a variable. Is there a way to prevent this behavior?
Whilst it won't affect your HTML page at all with the line breaks (unless you are using pre or text-wrap: pre), you should be able to call trim() on those variables to remove newlines.
To find out if your variable has a newline at front or back, try this regex
var_dump(preg_match('/^\n|\n$/', $variable));
(I think you have to use single quotes so PHP doesn't turn your \n into a literal newline in the string).
My guess is your variables are to blame. You might try cleaning them up with trim: http://us2.php.net/trim.
The line breaks show up because of multi-byte encoding, I believe. Try:
$newstring = mb_substr($string_w_line_break,[start],[length],'UTF-8');
That worked for me when strange line breaks showed up after parsing html.

Categories