{"id":2230,"date":"2013-02-08T05:39:29","date_gmt":"2013-02-08T05:39:29","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2230"},"modified":"2013-02-08T05:39:29","modified_gmt":"2013-02-08T05:39:29","slug":"php-remove-last-character-how-to-remove-last-character-from-string-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/php-remove-last-character-how-to-remove-last-character-from-string-examples\/","title":{"rendered":"How To Remove Last Character From String Using PHP &#8211; Examples"},"content":{"rendered":"<p>Some times while dealing with strings in PHP, it may be required to remove the last character of a string. In this article, I am going to share very easy ways of <em><strong>removing last character from string using PHP<\/strong><\/em> by means of multiple examples.<!--more--><\/p>\n<h2>How To Remove Last Character Using From String Using PHP Examples<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2230\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let us assume that we have a string with some random text. So lets look at the different available ways to remove the last character from string. Consider our sample string below:<\/p>\n<pre class=\"lang:php decode:true\" title=\"Sample String\">&lt;?php\n\n$my_string = 'Hello! This is my random string';\n\n?&gt;<\/pre>\n<h3>Example 1: By using rtrim<\/h3>\n<p>rtrim \u2014 Strips whitespace (or other characters) from the end of a string <strong>Command:<\/strong><\/p>\n<pre class=\"lang:php decode:true\">rtrim($string,'x');<\/pre>\n<p><strong>Usage:<\/strong><\/p>\n<pre class=\"lang:php decode:true\" title=\"Using rtrim\">\/*Example1:=====================================================================*\/\necho 'Example 1: Removing the Last character from string using PHP&lt;br \/&gt;';\necho \"Before using rtrim: &lt;br \/&gt;\";\n\n\/\/Sample String\n$my_string = 'Hello! This is my random string';\n\n\/\/Echo the sample string\necho $my_string . '&lt;br \/&gt;&lt;br \/&gt;';\n\n\/\/Apply rtrim function to delete the last character from string\necho \"After using rtrim: &lt;br \/&gt;\";\n\necho rtrim($my_string, 'g');\n\necho '&lt;br \/&gt;&lt;br \/&gt;';<\/pre>\n<h3>Example 2: By\u00a0using\u00a0substr_replace<\/h3>\n<p>substr_replace \u2014 Replace text within a portion of a string <strong>Command:<\/strong><\/p>\n<pre class=\"lang:php decode:true\" title=\"Using substr_replace\">substr_replace($string ,\"\",-1);<\/pre>\n<p><strong>Usage:<\/strong><\/p>\n<pre class=\"lang:php decode:true\" title=\"Example 2\">\/*Example2:=====================================================================*\/\necho 'Example 2: Removing the Last character from string using PHP&lt;br \/&gt;';\necho \"Before using substr_replace: &lt;br \/&gt;\";\n\n\/\/Sample String\n$my_string = 'Hello! This is my random string';\n\n\/\/Echo the sample string\necho $my_string . '&lt;br \/&gt;&lt;br \/&gt;';\n\n\/\/Apply rtrim function to delete the last character from string\necho \"After using substr_replace: &lt;br \/&gt;\";\n\necho substr_replace($my_string ,\"\",-1);\n\necho '&lt;br \/&gt;&lt;br \/&gt;';<\/pre>\n<h3>Example 3: By using substr<\/h3>\n<p>substr \u2014 Returns part of a string <strong>Command:<\/strong><\/p>\n<pre class=\"lang:php decode:true\" title=\"Using substr\">substr($string, 0, -1);<\/pre>\n<p><strong>Usage:<\/strong><\/p>\n<pre class=\"lang:php decode:true\" title=\"Example 3\">\/*Example3:=====================================================================*\/\necho 'Example 3: Removing the Last character from string using PHP&lt;br \/&gt;';\necho \"Before using substr: &lt;br \/&gt;\";\n\n\/\/Sample String\n$my_string = 'Hello! This is my random string';\n\n\/\/Echo the sample string\necho $my_string . '&lt;br \/&gt;&lt;br \/&gt;';\n\n\/\/Apply rtrim function to delete the last character from string\necho \"After using substr: &lt;br \/&gt;\";\n\necho substr($my_string, 0, -1);\n\necho '&lt;br \/&gt;&lt;br \/&gt;';<\/pre>\n<h3>Example 4: By Using\u00a0mb_substr<\/h3>\n<p><strong>Command:<\/strong><\/p>\n<pre class=\"lang:php decode:true\" title=\"Using mb_substr\">mb_substr \u2014 Get part of string<\/pre>\n<p><strong>Usage:<\/strong><\/p>\n<pre class=\"lang:php decode:true crayon-selected\" title=\"Example 4\">\/*Example4:=====================================================================*\/\necho 'Example 4: Removing the Last character from string using PHP&lt;br \/&gt;';\necho \"Before using mb_substr: &lt;br \/&gt;\";\n\n\/\/Sample String\n$my_string = 'Hello! This is my random string';\n\n\/\/Echo the sample string\necho $my_string . '&lt;br \/&gt;&lt;br \/&gt;';\n\n\/\/Apply mb_substr function to delete the last character from string\necho \"After using mb_substr: &lt;br \/&gt;\";\n\necho mb_substr($my_string, 0, -1);\n\necho '&lt;br \/&gt;&lt;br \/&gt;';<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2230\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p><strong>Your Turn!<\/strong><\/p>\n<p>Do you know of any other ways to use <strong><em>PHP to\u00a0remove last character from string<\/em>?<\/strong> Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some times while dealing with strings in PHP, it may be required to remove the last character of a string. In this article, I am going to share very easy ways of removing last character from string using PHP by means of multiple examples.<\/p>\n","protected":false},"author":1,"featured_media":141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,13],"tags":[33],"class_list":["post-2230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-tips-and-tricks","category-tips-and-tricks","tag-php-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-zY","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=2230"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2230\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=2230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}