• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Truncate Strings in php Using substr()

Truncate Strings in php Using substr()

March 24, 2022 Leave a Comment

To truncate a string variable in php, the easiest way is to use the php substr() function.

$variable = "this is a variable";

$truncated = substr($variable,0,6);

echo $truncated;

//Output:
this i

Another way to truncate a string is with the mb_strimwidth() function.

$variable = "this is a variable";

$truncated = mb_strimwidth($variable,0,6);

echo $truncated;

//Output:
this i

When working with string variables in our php programs, it is useful to be able to easily manipulate and change the value of the variables.

One such change is if you want to truncate a string and remove characters from the end.

To truncate a string in php, you can use the substr() function.

substr() takes three arguments (a string, a start position, and an ending position), and returns a substring of the given string.

To remove characters from the end of a string, you can use substr() by starting at 0 and ending at the position of the character you want to end your string at.

Below is an example of how to use substr() to truncate a string in php.

$variable = "this is a variable";

$truncated = substr($variable,0,6);

echo $truncated;

//Output:
this i

Truncating a String with mb_strimwidth() in php

Another way you can get a truncated string of a specified width is with the mb_strimwidth() function.

mb_strimwidth() takes in a string, starting position and ending position and returns a substring of the given string.

The one difference between substr() and mb_strimwidth() is you can pass one more parameter, ‘trim_marker’, which will concatenate given string after getting the substring.

Below is a simple example in php of how to truncate a string with mb_strimwidth().

$variable = "this is a variable";

$truncated = mb_strimwidth($variable,0,6, "...");

echo $truncated;

//Output:
this i...

Hopefully this article has been useful for you to learn how to truncate string variables in php.

Other Articles You'll Also Like:

  • 1.  Get Current Month of Date in php
  • 2.  php var_dump() Function – Print Information about Variable Type and Value
  • 3.  Delete Cookie Using php
  • 4.  php Convert Radians to Degrees with php rad2deg() Function
  • 5.  echo php – Output One or More Strings
  • 6.  How to Create an Empty Array in php
  • 7.  php acos – Find Arccosine and Inverse Cosine of Number
  • 8.  php is_string() Function – Check if Variable is String in php
  • 9.  Get Array Length with php count() Function
  • 10.  preg_replace php – Use Regex to Search and Replace

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x