How to take last word from string in php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    We are given a string. We are required to write a program in PHP to find the length of the last word in the string using inbuilt functions. We have already discussed approach of solving this problem here. This article discusses the PHP solution to the problem. Examples:

    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5

    We will mainly use these three inbuilt function in PHP to solve this problem:

    • substr() Function: This inbuilt function in PHP is used to extract a part of string.
    • strrpos() Function: This inbuilt function in PHP is used to find the last position of string in original or in another string. It returns the integer value corresponding to position of last occurrence of the string, also it treats uppercase and lowercase characters uniquely.
    • strlen() Function: This inbuilt function in PHP is used to find the length of a string.

    The idea to solve this problem using the above mentioned inbuilt function is to first find the position of last occurring space in the string using the strrpos() function. After getting the position of last occurring space we can easily get the last word in the string using the substr() function and store this in a new string variable. At last, we can use the strlen() function to find the length of the last word in the string. 

    PHP

        function length_last_word($string)

        {  

            $pos = strrpos($string, ' ');

            if(!$pos)

            {

                $pos = 0;

            }

            else

            {

                $pos = $pos + 1;

            }

            $lastWord = substr($string,$pos);

            return strlen($lastWord);

        }

        print_r(length_last_word('geeksforgeeks')."\n");

        print_r(length_last_word('computer science portal')."\n");

    ?>

    Output:

    13
    6

    How to take last word from string in php?

    How to remove the last word from a string in php?

    Are you searching for How to remove the last word from a string in php?

    Then you are in the right place.

    Sometimes we need to remove the last word from a string in php for various purposes. We can do it in many ways.

    In this GeekySolution, I will show you How to remove the last word from a string in php?

    The technique which I am going to show you here is really very easy but powerful. Also if you want, you can modify and customize these code easily according to your need and make it more powerful.

    In this GeekySolution, you will learn...

    • How to remove the last word from a string using php?
    • Complete code to remove the last word from a string in PHP:
    • Common problems and solutions about: “How to remove the last word from a string in php?”
    • How to remove the last word from String in php?
    • How to remove the last word from a string in php?
    • How to remove the last word from a string in php?
    • How to strip whitespace or other characters from the end of a string in php?
    • How to find the length of the last word in string in php?
    • How to remove the last word from a string in php?
    • You are valuable to us

    Time needed: 2 minutes.

    To remove the last word from a string in php just follow the steps below:

    1. Specify the string from which we want to remove

      Specify the string from which we want to remove using this code:
      $str1 = ‘GeekySolution is awesome’;

    2. Remove the last string

      Remove the string using this code:
      echo preg_replace(‘/\W\w+\s(\W)$/’, ‘$1’, $str1).”\n”;

    Complete code to remove the last word from a string in PHP:

    To remove the last word from a string using php just use the code below:

    How to use this code?

    Insert the above php code to your php file.

    Common problems and solutions about: “How to remove the last word from a string in php?”

    How to remove the last word from String in php?

    GeekySolution:

    To remove the last word from String in php just use this code-

    How to use this code?

    Insert the above php code to your php file.

    How to remove the last word from a string in php?

    GeekySolution:

    To remove the last word from a string in php just use this code-

    How to use this code?

    Insert the above php code to your php file.

    How to remove the last word from a string in php?

    GeekySolution:

    To remove the last word from a string in php just use this code-

    How to use this code?

    Insert the above php code to your php file.

    How to strip whitespace or other characters from the end of a string in php?

    GeekySolution:

    To strip whitespace (or other characters) from the end of a string in php just use this code-

    ";
    
    // Specify the string
    $str2 = "GeekySolution is awesome";
    
    // Remove extra white space at the right and show it
    echo "String after removal of character or word at the end or right: " .  rtrim($str2,"awesome");
    ?>

    How to use this code?

    Insert the above php code to your php file.

    How to find the length of the last word in string in php?

    GeekySolution:

    To find the length of the last word in string in php just use this code-

    ";
    
    // Last word start position
    $last_word_start = strrpos ( $str , " ") + 1;
    
    // Last word end position
    $last_word_end = strlen($str) - 1;
    
    // Get the last word
    $last_word = substr($str, $last_word_start, $last_word_end);
    
    // Show the last word
    echo "The last word is: " . $last_word . "
    "; // Get the length of last word $length_of_last_word = strlen($last_word); // Show the length of last word echo "The length of last word is: " . $length_of_last_word; ?>

    How to use this code?

    Insert the above php code to your php file.

    How to remove the last word from a string in php?

    GeekySolution:

    To remove the last word from a string in php just use this code-

    How to use this code?

    Insert the above php code to your php file.

    References:

    1. https://stackoverflow.com/questions/29428459/php-how-can-i-remove-from-string-the-last-word
    2. https://www.w3resource.com/php-exercises/php-regular-expression-exercise-2.php
    3. https://codereview.stackexchange.com/questions/8683/remove-last-word-from-string
    4. https://www.codeproject.com/Questions/838096/How-can-I-remove-last-word-from-a-string
    5. http://phpcorelab.com/php-remove-last-word-from-string/

    You are valuable to us

    If you like this post then share it on Facebook, Twitter or any other social network with your friends so that other people can get help from this post.

    If you have any question or suggestion about this post then write a comment below.

    • Author
    • Recent Posts

    How to take last word from string in php?

    Hi, I'm James. I'm a technology lover.
    I like to learn about modern technologies. I like software programming, web development, graphics design, and blogging.

    Knowledge is power. So always I try to learn in any situation from anyone.

    How to take last word from string in php?

    How to find the length of the last word in string in php? How to remove last word from a string in php? How to remove last word from string in php? How to remove the last word from String in php? How to strip whitespace (or other characters) from the end of a string in php? PHP code that removes the last word from a string

    How can I get the first word of a string in PHP?

    echo "The first word of string is: " . $arr [0]; ?> Method 3: Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string.

    What is use of trim in PHP?

    Definition and Usage. The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.

    How can I remove last 5 characters from a string in PHP?

    You can use the PHP substr_replace() function to remove the last character from a string in PHP. $string = "Hello TecAdmin!"; echo "Original string: " . $string .