faqts : Computers : Programming : Languages : PHP : Common Problems : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 18 people (94%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How to remove the n last characters out of a string ?

Mar 23rd, 2001 18:53
Ben Udall, Philippe Benaiche, http://www.php.net/manual/en/function.substr.php


If you give a negative length to the substr function, it starts from 
the end of the string.
$str = "Hello world";
$num = 6;
echo substr($str, 0, -$num);
This outputs "Hello";