In this post, I will show you how to delete the first character in a string in PHP.
PHP is an incredible programming language but it is often difficult to find out how to do the most basic things. For example, in this case, we want to delete the first character in a PHP string and we are probably directed to this SUBSTR PHP page. These PHP help pages provide us with a lot of information but often do not answer our basic questions.
So, today, I will tell you exactly how to delete the first character in a PHP string and give you an example page.
(As I am a technical trainer by profession, I may be bringing many more of these PHP tutorials–I have been dealing with all sorts of PHP string issues of late. If you would like these PHP tutorials automatically delivered to you, you may subscribe to my website, Richard Cummings, below.
PHP: How to Delete the First Character in a PHP String
Ok, so let’s pick a term, any term. How about “Richard enjoys PHP but finding answers to questions can be difficult.” ?
Now, our goal is to remove the “R”. So, at the end of this lesson, I will be ichard.
We can delete the first character of a string in PHP in three easy lines using the PHP substr utility.
Here are the lines which I will explain below:
<? // this file shows how to remove the first character of a string $thename="Richard enjoys PHP but finding answers to questions can be difficult."; $thename = substr($thename, 1); echo $thename; ?>
Removing the First Character in PHP: Explained
This file does not need a whole lot of explanation. Obviously, in the first line, we simply declare our variable. Then, we use the PHP substr function to remove the first letter “R”.
The key here is that the string characters begin at zero. Thus, the “1” represents the second character of the string (very much like an array). By simply placing this “1” there and nothing after it, we tell PHP to return the string starting at the second character and proceeding until the end of the string.
To see this file in action, simply copy and paste the text into your favorite text editor (UltraEdit is by far the best), name the file with a php extension, and then upload it to your web server.
If you would like to then do some experimentation, change the number 1 to something else and see how things turn out. Or, you may want to try to add a comma and then a number after 1 such as this:
$thename = substr($thename, 1,4);
The number “4” above tells PHP to return 4 characters instead of defaulting to provide the rest of the PHP string.
Now, how can you use substr to return the string “ichard enjoys PHP”.
With this simple little file, the PHP substr page begins to make much more sense.
Want to get really brave? Throw in some negative numbers and see what happens…
PHP Delete First Character In String: Conclusion
I hope that this page has helped everyone out there who is wanting to know how to use PHP to delete the first character in a string.
Again, if you would like to be automatically emailed any PHP goodies to come, simply subscribe below.