Hi,

I want to remove last character of the string like if i have string

$str= "abc cyz xyz erz";

now i want to remove last character 'z' from string.

Note: z is also present in string on diffrent locations.

    You can use [MAN]substring[/MAN] to find parts of a string.

      yes we can find last char with substr

      
      $text= "abc cyz xyz erz";  
      $length = strlen($text); $fname=trim(substr($text,$length-1,$length)); print $fname; //now when we go to remove this last char it remove all z its the problem i need //to remove only last x :mad: $fname=str_replace($fname, " ", $text);
      print $fname;

        Now i need to delete last character how can i delete this ?

          My code does delete the last character!!

          You're making this harder than it is!!

          $string = "abc def xyz qaz";
          
          $string = substr($string, 0, -1);

          HOw hard is that? Why use Strlen? It's not necessary.

          The reason I used $text and $new_text was to illustrate which line actually did the work so it wasn't confusing. But you could write a function to just get rid of the last character.

          <?php
          
          function removeChars($string)
          {
              $string = substr($string, 0, -1);
              return $string;
          }
          
          ?>

          Get it? If you just want the last letter, that's different. But if you want the entire string previous to the last character, I believe my code is the most expeditious way of achieving that.

          ~Brett

            Indeed Bpat1434 - if the OP took the time to read the manual page which I conveniently linked for him he'd know that too.

              Ease up there Brett,

              Save the exclamation marks for sometime when you really need them.

              Your code is more efficient than mine. I've learned something.

              Then again, I never said your code wouldn't work. I never commented on your code.

              To tell you the truth, I never even read your code. I just scrolled down to what was then the end of the thread, saw a simple question which I could answer, and I answered it.

              It's not the sort thing you should get too worked up over.

                Yes, but you dont' need to answer questions to ones that already have an answer, or a few for that matter.

                And my exclamation marks were warranted. Mainly because:

                To tell you the truth, I never even read your code. I just scrolled down to what was then the end of the thread, . . .

                The point of threading conversations is to read what others have already offered for advice. Then, if you can offer some insight, or a different point of view on the already given answer you post.

                I didn't mean to go off on you, or anyone else, but it's the basic principle of looking before you leap, reading before you post, thinking before you say.... I only got upset because...

                gadeer_ahmed
                yes we can find last char with substr

                justsomeone
                $string="abc def xyz qaz";
                $string=substr($string,0,(strlen($string)-1));
                echo $string;

                I don't see any major difference(s) between the two ideas. Perhaps some explanation would have helped, but you just said you can find it with substr and strlen, that much he knew.

                I apologize, but can we please be a little better of reading at most the "useful looking" posts? I mean, reading ones that either post code or seem to be helpful. Maybe you don't have to read word for word, but skimming to see if an answer was given would be better. What I would hate to see (and what I have seen elsewhere) is where one answer is given, then another, and another, and no-one wants to help the guy learn, but just wants their help acknowledged and that they helped him and no-one else. We're here to learn (I'm glad you learned something) and let's just remember that in order to learn from eachother, we must READ their posts.

                Apologies...

                ~Brett

                  No worries.

                  Since I bothered to look in, I should as you said, have read the full thread.

                  In fact I got into the thread by mistake. I had intended to click on one with no replies, so when I saw that the OP was "being dealt with", i was going to close down again. but when I saw a simple, open question at the end, I figured it wouldn't do any harm to answer it.

                  Anyway no harm done, I hope. The OP got his answer, I learned a nifty trick, and your answer remains the best one.

                  Cheers. 🙂

                    Write a Reply...