PHP Strings Primer - Multiple Lines
(Page 16 of 37 )
Often times when we accept input from a user, we need that input to span multiple lines. There are a couple different situations when causing text to span multiple lines is needed. The first is when the user specifies that there should be a line break in text, the second when the text entered would run longer on one line than we would prefer.
Using nl2brIn our first situation, a common example would be when a user enters text within a text box. Each time the user presses the enter key inside the text box, a line break is added to the data. When we receive the data from the form, a newline character will represent each line break. If we were to display this data directly back to the user's browser it would not appear to have the line breaks. The reason for this is that HTML requires a '<br />' in order to display a line break.
The first function we will look at in this section, 'nl2br()', will provide the necessary breaking return tags. Take the following string for an example.
<?php $mystring = "First line\nSecond line"; ?> |
If we were to display that to the browser, with echo, it would appear as such:
Obviously, this is not as we intended. So, we simply run the string through the 'nl2br()' function and our output will look correct.
<?php $mystring = "First line\nSecond line"; echo nl2br($mystring); ?> |
Output:
In this first example, the value of $mystring is not changed. We have simply displayed the result from the 'nl2br()' function. In some cases it may be desirable to modify the original variable with the result of the 'nl2br()' function. Let's see an example of how we would do that.
<?php $mystring = "First line\nSecond line"; $mystring = nl2br($mystring); echo $mystring; ?> |
This would output correctly as:
wordwrapThe second situation where we would want to break a string into multiple lines would be to ensure that the string does not run longer than we care for it to on any one line. An example would be in a guestbook that is displayed in a table. If someone were to sign the guestbook and intentionally place a very long word that would not automatically wrap, our table layout would be ruined. The wordwrap function will take care of this for us however.
With the wordwrap function, we can specify that lines should not run longer than a certain amount, and we can force them to break to a new line. Let's take a look at an example.
<?php $mystring = "Supercalifragilisticexpialidocious!"; echo $mystring; ?> |
This would output:
Supercalifragilisticexpialidocious! |
If this word was to be displayed in a small table cell where there was only room for 10 characters, our table would be out of alignment. Let's now look at how the wordwrap function can solve this problem.
<?php $mystring = "Supercalifragilisticexpialidocious!"; echo wordwrap ($mystring, 10, "<br />\n", 1); ?> |
The output would be:
Supercalif ragilistic expialidoc ious! |
As you can see, this is what we were looking for. Now, our table layout will be preserved, and we were still able to display the data.
The wordwrap function provides you quite a bit of flexibility on how it works. In the example above, we passed the function 4 parameters. The first is the string to operate upon. The second parameter is the width that we want the string to be wrapped to. For the third parameter, the separator, we passed "'<br />\n'". This parameter is inserted at each break point. We could have used any character, or combination of characters, that we chose. The last parameter tells the function whether to do a hard or soft wrap.
A hard wrap, specified by the integer 1, tells the function to wrap the text at the specified width in any case. By specifying the integer 0, for a soft wrap, the function will wrap like a standard word processor and break the text up between words. In our example above, a soft wrap would not have been sufficient as there was only one word.
Let's take a look at the function prototype so that the description will make more sense to you.
string wordwrap( string input [, int width [, string separator [, int wraptype]]]) |
The brackets around the second, third, and fourth parameters indicate that they are optional. If they are not specified, the width will be set to 75, the separator will be a newline, and the wrap type will default to a soft wrap.
We could have accomplished the same task with the 'chunk_split()' function. It performs almost the same duty as the 'wordwrap()' function, with a couple of differences. First, the 'chunk_split()' function always performs a hard break. For this reason, a fourth parameter is not necessary as with wordwrap. The other difference between the two functions is that 'chunk_split()' adds the separator at the end of the string, as well as at each break point. So that you may compare with the prototype above, we will now include the prototype for the 'chunk_split()' function.
string chunk_split( string input [, int width [, string separator]]) |
Next: Data Preparation >>
More Programming Basics Articles
More By Matt Wade