Formatting strings in PHP

  Tutos   -   28/07/2015


This tutorial will show you how to format a string in PHP. Here we talk about line breaks, hyphenation, spaces, upper and lower case.

The different snippets that we will see apply to a string and are:
  • Add line breaks in HTML to each line in a text
  • Put a line break to a string after a certain length (cutting the words or not ...)
  • Add spaces in HTML
  • Put all the letters in capital
  • Put all the letters in lower case
  • Put the first letter capitalized and all other in lowercase
  • Put all the initial letters capitalized and the rest lowercase for each word

Add line breaks in HTML to each line in a text

This code will be useful for example when you use a textarea and you want to show the content in HTML later. If you try to display it directly, there will be no line break and the text will be displayed as a single block.
To display line breaks in HTML, just use the PHP function nl2br, which replaces line breaks characters \n and \r to line breaks in HTML <br>.

Examples using the nl2br function:

// Example #1
$string = "Line 1\nLine 2\nLine 3";
echo nl2br($string);

// Example #2
$string = "Line 1
Line 2
Line 3";
echo nl2br($string);


Put a line break to a string after a certain length

To put line breaks to a string after a specific length, just use the PHP function wordwrap.

The first parameter is the string and the second the length desired.
The third parameter is the the line break type. It is optional and its default value is \n. If you want to display the text in question in HTML, it is better to immediately put <br>. Otherwise you will have to go later through the nl2br function.
The fourth and final parameter is an optional Boolean indicating whether the words should be cut or not if the length is reached before completion.

$string = "This is the text that we will cut with this looooooooooong word.";
echo "#1 :".wordwrap($string, 10)."<br>";
echo "#2 :".wordwrap($string, 10, "<br>")."<br>";
echo "#3 :".wordwrap($string, 10, "<br>", true)."<br>";

Result:

#1 :This is the text that we will cut with this looooooooooong word.

#2 :This is
the text
that we
will cut
with this
looooooooooong
word.

#3 :This is
the text
that we
will cut
with this
looooooooo
oong word.


Add spaces in HTML

The HTML space character is very simple, it's &nbsp;. You can put as much as of it that you want to make a tab for example. It remains, however, recommended to use the style margin, padding or text-indent.

Put all the letters in capital


$string = "Here is a SMALL test sentenCE.";
echo strtoupper($string);

The result will be:
HERE IS A SMALL TEST SENTENCE.

Put all the letters in lower case


$string = "Here is a SMALL test sentenCE.";
echo strtolower($string);

The result will be:
 here is a small test sentence.

Put the first letter capitalized and all other in lowercase


$string = "Here is a SMALL test sentenCE.";
echo ucfirst($string);

The result will be:
 Here is a small test sentence.

Put all the initial letters capitalized and the rest lowercase


$string = "Here is a SMALL test sentenCE.";
echo ucwords($ string);

The result will be:
Here Is A Small Test Sentence.

Note that special characters such as 'é', 'à', 'ù' or 'è' are not converted.
However, you can get rid of special characters of this type using a function such as this that replace 'ç' to 'c' and 'ù' to 'u':
$string = "How are things where are you?.";
$string = preg_replace(array('/ç/', '/ù/'), array('c','u'), $string);


Tags :  
PHP
  


Autodidact passionate about the web, I'm always looking for new challenges.
Founder of Online-Free-Tools.com.



  Comments


No comment

Add a comment




 FR     EN  

Copyright 2024 -   Contact us - 2024-11-21 14:14:00