Title: Learn How To Remove Letters From End Of String PHP with substr() Function
Description: learn how to remove letters from end of string php with substr function PHP
Tags: learn, how, to, remove, letters, from, end, of, string, php, with, substr, function
Info: This Post Has Been Viewed 15550 Times SinceThu Apr 12, 2007 4:59 pm Author hostman With 11 Replies #1053

Learn How To Remove Letters From End Of String PHP with substr() Function

if you want to know how you can remove characters from the end of a string in php you can do it with the substr() funcion. lets say we have a string that its equal to "Free Wallpapers At Walpaperama Website"

so lets say i want to remove the last four letters (or characters) from my string so this is how i would get those letters out.

     Code:
<?php
$string = "Free Wallpapers At Walpaperama Website";
$newstring = substr($string, 0, -4);
echo $newstring;
?>

the output will look like this:
     Code:
Free Wallpapers At Walpaperama Web


Note that the number -4 means how many character back you are going to be removing on the string. that's it, we are finish with this small simple tutorial step by step guide. hope you learned something new today about how to do programming in php to be able to get out the last letters or numbers from a sentence or a string in your scripts code this way you can do your own script to show or display the results on your own website.

substr() is avaliable on PHP 4 and PHP 5

substr — Return part of a string
Description: string substr ( string $string, int $start [, int $length] )

substr() returns the portion of string specified by the start and length parameters.

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

if you want to learn more about the substr() function you can visist php.net
Comments (11)
View Top Comments
Leave Your Comments...
#1
1
Dave:
4 years ago
#26012
Dave Mon Oct 29, 2007 8:35 am
this is really useful, thanks!
#2
Roel:
4 years ago
#28065
Roel Sat Nov 17, 2007 10:18 am
that is a clear text about this particular fuction :)
#3
php:
3 years ago
#43537
php Thu Feb 28, 2008 6:29 pm
-xicpaiuugwwo7rxpppcml1rnurl6ailtp-2mpq96qej63sd7n
thank you for your help on this. i learned alot
#4
Benjamin:
3 years ago
#48026
Benjamin Mon Mar 31, 2008 4:18 pm
can i also search for a string or a character from the end of that string?
#5
Stu:
3 years ago
#52310
Stu Thu May 01, 2008 9:01 am
many thanks, just what was need!
#6
Peter:
2 years ago
#120691
Peter Mon Nov 16, 2009 11:31 am
you rock dude!
#7
1
Ed:
2 years ago
#125351
Ed Tue Jan 05, 2010 12:31 am
thats awesome!

#8
lamont:
1 year ago
#132938
lamont Wed May 05, 2010 10:26 am
Major thanks! I've been looking for this for awhile now.
#9
Jassen:
1 year ago
#141607
Jassen Tue Aug 10, 2010 2:22 pm
Verbatim what I was looking for, thank you so much!
#10
Dor:
2 weeks ago
#174470
Dor Sat Jan 21, 2012 3:46 pm
Your page helped me. thank you.
#11
Vimal:
4 hours ago
#175881
Vimal Thu Feb 09, 2012 9:05 pm
I has been doing a multiple search functionality where all search text are optional. I has been pulling out my hair for long 4-5 hours because of fixing an extra 'and' coming at the end of query and wrote lots of code,but in vain.

So you know how much it was helpful for me from your tutorial.I removed that extra 'and' with help of this tutorial and my code is now short and compact. Thanks a LOT
Leave Your Comments...
Share
| More