{"id":115,"date":"2019-09-16T16:32:21","date_gmt":"2019-09-16T16:32:21","guid":{"rendered":"https:\/\/tutorialio.com\/?p=115"},"modified":"2025-11-04T10:31:59","modified_gmt":"2025-11-04T10:31:59","slug":"remove-first-or-last-character-from-a-string-in-php","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/remove-first-or-last-character-from-a-string-in-php\/","title":{"rendered":"How Can I Remove the First or Last Character from a String in PHP?"},"content":{"rendered":"<p>Every now and then, we have to deal with strings which contain unwanted characters either at their beginning or the end. PHP provides a lot of methods which can be used to remove these characters. Each of them has its advantages and disadvantages. In this tutorial, we will discuss all these methods in detail so that you can choose the one that works best for your situation.<\/p>\n<h2 id=\"remove-last-n-characters-from-a-string-using-substr\">Remove Last \u2018n\u2019 Characters from a String Using substr()<\/h2>\n<p>The <a href=\"..\/string\/substr-function.php\">PHP substr() function<\/a> returns the part of string specified by the <code>start<\/code> and <code>length<\/code> parameters. If you want to remove characters from the end of string, you can set the value of <code>start<\/code> to 0 and the value of <code>length<\/code> to a negative number.<\/p>\n<p>You can set the value of <code>length<\/code> to -1 in order to remove the last character of a string. Similarly, you can set the <code>length<\/code> to -2 in order to remove the last two characters from a string. To put it simply, set the value of <code>length<\/code> property to be negative of the number of characters that you want to remove from the end of the string.<\/p>\n<p>In the following example, we have used the <code>substr()<\/code> function to remove different number of characters from the end of string.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$sentence = \"An apple a day keeps anyone away if you throw it hard enough.\";\n\n$removed_last_one   = substr($sentence, 0, -1);\n$removed_last_two   = substr($sentence, 0, -2);\n$removed_last_three = substr($sentence, 0, -3);\n$removed_last_four  = substr($sentence, 0, -4);\n$removed_last_twenty  = substr($sentence, 0, -20);\n\n\/\/ Output \u2014 An apple a day keeps anyone away if you throw it hard enough\necho $removed_last_one;\n\n\/\/ Output \u2014 An apple a day keeps anyone away if you throw it hard enoug\necho $removed_last_two;\n\n\/\/ Output \u2014 An apple a day keeps anyone away if you throw it hard enou\necho $removed_last_three;\n\n\/\/ Output \u2014 An apple a day keeps anyone away if you throw it hard eno\necho $removed_last_four;\n\n\/\/ Output \u2014 An apple a day keeps anyone away if you t\necho $removed_last_twenty;<\/code><\/pre>\n<h2 id=\"remove-first-n-characters-from-a-string-using-substr\">Remove First \u2018n\u2019 Characters from a String Using substr()<\/h2>\n<p>You can also use the <a href=\"..\/string\/substr-function.php\">substr() function<\/a> to remove characters from the beginning of a string by specifying a non-zero value for the <code>length<\/code> parameter. The <code>length<\/code> parameter is optional in this function. Omitting it will return the whole string starting from <code>start<\/code> index.<\/p>\n<p>To remove the first character from the beginning of string, set the value of <code>start<\/code> to 1. To remove the first two characters from the beginning, set the value of <code>start<\/code> to 2. You can remove the first \u2018n\u2019 characters from a string by setting the value of <code>start<\/code> to \u2018n\u2019 in a similar manner.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$sentence = \"An apple a day keeps anyone away if you throw it hard enough.\";\n\n$removed_first_one   = substr($sentence, 1);\n$removed_first_two   = substr($sentence, 2);\n$removed_first_three = substr($sentence, 3);\n$removed_first_four  = substr($sentence, 4);\n$removed_first_twenty  = substr($sentence, 20);\n\n\/\/ Output \u2014 \"n apple a day keeps anyone away if you throw it hard enough.\"\necho $removed_first_one;\n\n\/\/ Output \u2014 \" apple a day keeps anyone away if you throw it hard enough.\"\necho $removed_first_two;\n\n\/\/ Output \u2014 \"apple a day keeps anyone away if you throw it hard enough.\"\necho $removed_first_three;\n\n\/\/ Output \u2014 \"pple a day keeps anyone away if you throw it hard enough.\"\necho $removed_first_four;\n\n\/\/ Output \u2014 \" anyone away if you throw it hard enough.\"\necho $removed_first_twenty;<\/code><\/pre>\n<h2 id=\"remove-specific-characters-from-the-end-of-string-using-rtrim\">Remove Specific Characters from the End of String Using <code>rtrim()<\/code><\/h2>\n<p>The <a href=\"..\/string\/rtrim-function.php\">rtrim() function in PHP<\/a> strips away all characters from the end of given string which are specified in the <code>character_mask<\/code>. If you don\u2019t set a value for <code>character_mask<\/code>, this function will remove NULL, tab, new line, vertical tab, carriage return and ordinary white space.<\/p>\n<p>It is important to remember that <code>rtrim()<\/code> keeps removing characters from the end as long as they also exist in the <code>character_mask<\/code>. The following examples should make it clear:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$sentence = \"How are you feeling Today????!!!,,,...   \";\n\n\/\/ Output \u2014 \"How are you feeling Today????!!!,,,...\"\necho rtrim($sentence);\n\n\/\/ Output \u2014 \"How are you feeling Today????!!!,,,\"\necho rtrim($sentence, ' .');\n\n\/\/ Output \u2014 \"How are you feeling Today????!!!,,,...\"\necho rtrim($sentence, ' ,');\n\n\/\/ Output \u2014 \"How are you feeling Today????!!!\"\necho rtrim($sentence, ' .,');\n\n\/\/ Output \u2014 \"How are you feeling Today\"\necho rtrim($sentence, '?!., ');<\/code><\/pre>\n<p>In the first case, we have not specified a value for the <code>character_mask<\/code>. Therefore, the <code>rtrim()<\/code> function only removes spaces from the end.<\/p>\n<p>In the second case, the <code>character_mask<\/code> contains a space(<code><\/code>) and full stop (<code>.<\/code>). Therefore, the <code>rtrim()<\/code> function keeps removing the last characters as long as it is either a full stop or a space.<\/p>\n<p>In the third case, the <code>rtrim()<\/code> function stops at the full stop because it is not included in the character mask.<\/p>\n<p>In the last case, the <code>rtrim()<\/code> function keeps removing all the spaces, full stops, comma, exclamation and question marks until it reaches an alphabetic character.<\/p>\n<h2 id=\"remove-specific-characters-from-the-beginning-of-string-using-ltrim\">Remove Specific Characters from the Beginning of String Using <code>ltrim()<\/code><\/h2>\n<p>You can use the <a href=\"..\/string\/ltrim-function.php\">ltrim() function<\/a> if you want to remove specific characters from the beginning of a string. This function works exactly like the <code>rtrim()<\/code> function but it remove characters present at the beginning.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$sentence = \",,,...!!!###???How are you feeling Today?\";\n\n\/\/ Output \u2014 \"...!!!###???How are you feeling Today?\"\necho ltrim($sentence, ',');\n\n\/\/ Output \u2014 \"!!!###???How are you feeling Today?\"\necho ltrim($sentence, '.,');\n\n\/\/ Output \u2014 \"...!!!###???How are you feeling Today?\"\necho ltrim($sentence, ',!');\n\n\/\/ Output \u2014 \"How are you feeling Today?\"\necho ltrim($sentence, '?!.,#');<\/code><\/pre>\n<p>In the first case, the <code>ltrim()<\/code> function removes all the <code>,<\/code> that occur at the beginning of a the sentence or string. It is worth remembering that the function doesn\u2019t stop after removing a single character. That is the reason it removed all the comma characters at the beginning and not just the first comma.<\/p>\n<p>In the second case, the <code>ltrim()<\/code> function removed all the full stops and commas. It only stopped when it reached the exclamation marks. <\/p>\n<p>The output in third and fourth case can be explained similarly.<\/p>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let\u2019s recap everything that we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>You can use the <code>substr()<\/code> function if you want to remove last \u2018n\u2019 characters from a string. All you have to do is provide a negative value for <code>length<\/code>.<\/li>\n<li>You can also remove the first \u2018n\u2019 characters from a string using the <code>substr()<\/code> function. This time, you will have to provide a value for the <code>start<\/code> parameter and leave <code>length<\/code> untouched.<\/li>\n<li>If you only want to remove some specific characters from the beginning of a string, you can use the <code>ltrim()<\/code> function. It will remove all occurrences of given characters irrespective of how many times they repeat. <\/li>\n<li>If you want to remove some specific characters from the end of a string, you can use the <code>rtrim()<\/code> function. It will also remove all occurrences of given characters irrespective of how many times they repeat.<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know some other techniques to remove a given number of characters from the beginning or end of string in PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every now and then, we have to deal with strings which contain unwanted characters either at their beginning or the end. PHP provides a lot of methods which can be used to remove these characters. Each of them has its advantages and disadvantages. In this tutorial, we will discuss all these methods in detail so [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-how-to-guides"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":10242,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions\/10242"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}