All TalkersCode Topics

Follow TalkersCode On Social Media

PHP Limit String To 50 Characters

Last Updated : Jan 1, 2023

PHP Limit String To 50 Characters

In this tutorial we will show you the solution of php limit string to 50 characters, today we are going to understand how to limit string to 50 characters in php.

We can limit a string in php to n numbers with its built-in-functions. There are many ways with help of which we can create or apply limit.

Let us understand this today.

Step By Step Guide On Php Limit String To 50 Characters :-

Here, there are many ways to done this and also there are many inbuilt functions which we can use to limit string. For example, we can use wordwrap(), we can use substr, etc.

In this article, we are going to use substr and understand how to limit string using this.

<!DOCTYPE html>
      <html>
        <head>
          <title> php limit string to 50 characters </title>
   </head>
 <body>
 <?php
echo substr("Hello world",0,50)."<br>";
echo substr("Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's
standard dummy text ever since the 1500s, when an unknown
printer took a galley of type and scrambled it to make a
type specimen book. It has survived not only five centuries,
but also the leap into electronic typesetting,
remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset
sheets containing Lorem Ipsum passages, and more recently
with desktop publishing software like Aldus PageMaker
including versions of Lorem Ipsum.",0,50)."<br>";
// use variable to store chracters
echo substr("$variable_name",0,50)."<br>";
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, in the body part as you see that we done same method in different three ways. First ways is to input less character than required characters and see that what happens in output.
  6. In next we use more than or equal to 50 character and then see the output. You see that we directly insert 50 characters to substr function.
  7. In last, there is one more way in which we store the input in a variable and use that variable for substr function. Yes, we also can use substr in this way. The result remains same and it without variable.
  8. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with help of this article we are able to limit string to 50 characters in php.

I hope this tutorial on php limit string to 50 characters helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials