Newline inside variable string - php

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.

Related

imagettftext \\n line break puzzle

in PHP imagettftext, should I be able to make line breaks with "\n" in a string like this?
// The text to draw
$text = 'Something\nElse';
When I try this, the text outputted reads "Something\\nElse" with 2 back slashes before the n.
This does not result in a line-break.
Firstly, SHOULD a single slash n make a line break (\n)?
Secondly, How do i get it to work?
Thanks
\n is a line break only when the string is delimited with double quotes.
echo 'Something\nElse'; // outputs Something\nElse
echo "Something\nElse"; // outputs Something
// Else

PHP doesn't detect white space in string

I'm working on transferring data from one database to another. For this I have to map some values (string) to integers and this is where I run into a strange problem.
The string looks like this $string = "word anotherword"; so two words (or one space).
When I explode the string or count the amount of spaces it misses the white space. Why? I var_dumped the variable and it says it's a string.
Below is the code i'm using.
echo "<strong>Phases</strong>: ".$fases = mapPhase($lijst[DB_PREFIX.'projectPhase']);
The string that's being send to the function is for example "Design Concept". This calls the following function (where the spaces get ignored)
function mapPhase($phases){
echo "Whitespace amount: ".substr_count($phases, ' ')."<br />";
}
For the example string given this function echoes 0. What's causing this and how can i fix it? The strangest thing is that for one instance the function worked perfectly.
More than one whitespaces (in HTML) are always converter into one whitespace. For example code indents.
If you want to print more than one, one by one use &nbps; instead.
function mapPhase($phases){
echo 'Whitespace amount: '.substr_count($phases, ' ').'<br />';
}
It may well be that the alleged space in the string may not be a space as in ' ', but something similar, which gets rendered in the browser in the same way as ' ' would. (for a rudimentary list of possible characters: http://php.net/manual/en/function.trim.php)
Thus, checking what the whitespace exactly is may be the solution to that problem.
Maybe they are not even spaces. Try ord() for each symbol in your string.
ord(' ') is 32.
You can use:
$string = preg_replace('/\s+/', '', $string);

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.

Remove line from end of file with PHP

How would I remove a line from the end of a string with PHP? Thanks.
You want to remove whitespace? Try trim(). It's close cousins ltrim and rtrim may be closer to what you want.
If you are looking to remove the last line is a string containing new line character (\n), then you'd do something like this ...
$someString = "This is a test\nAnd Another Test\nAnd another test.";
echo "SomeString BEFORE=".$someString."\n";
// find the position of the last occurrence of a \n
$firstN = strlen($someString) - strpos(strrev($someString), "\n");
// get rid of the last line
$someString = substr($someString, 0, $firstN);
echo "SomeString AFTER=".$someString;
if you're trying to remove a line from an end of a file, you could try using PHP's file() function to read the file (which places it into an array) and then pop the last element. This is assuming that php is recognizing the line endings in your file.
$subject = 'Text
written
with
lines';
echo preg_replace('/\\n[^\\n]*\\n?$/', '', $subject);
With feature, that there can be one special newline at the end which is ignored, 'cause it is often a good convention to let one at the end of a file (assuming you are reading string from a file).

automatic line-break of textarea inside a form possible with 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.

Categories