44
$string = "Hello World Again".
echo strrchr($string , ' '); // Gets ' Again'

Now I want to get "Hello World" from the $string [The substring before the last occurrence of a space ' ' ]. How do I get it??

11 Answers 11

64
$string = "Hello World Again";
echo substr($string, 0, strrpos( $string, ' ') ); //Hello World

If the character isn't found, nothing is echoed

Sign up to request clarification or add additional context in comments.

Comments

13

This is kind of a cheap way to do it, but you could split, pop, and then join to get it done:

$string = 'Hello World Again';
$string = explode(' ', $string);
array_pop($string);
$string = implode(' ', $string);

2 Comments

this is a horrible way to do this especially since there is a command getting the last occurrence of a certain character (strrpos)
This is, on the contrary, a quite expensive way to do it.
7

One (nice and chilled out) way:

$string = "Hello World Again";
$t1=explode(' ',$string);
array_pop($t1);
$t2=implode(' ',$t1);
print_r($t2);

Other (more tricky) ways:

$result = preg_replace('~\s+\S+$~', '', $string);

or

$result = implode(" ", array_slice(str_word_count($string, 1), 0, -1));

8 Comments

@sdleihssirhc thanks for writing that comment because otherwise people would now not know who was the fastest kid here. thats right, vote for me people.
@locrizak dude, 3 operations not 4.
@locrizak check the 1 operation kung fu and weep.
Just because something is written in one line does not make it one operation. The preg_replace is one operation but is specific to whitespace (the question asked about any character).
@Yehonatan Read the damn question title. "last occurrence of a character"
|
6

strripos — Find the position of the last occurrence of a case-insensitive substring in a string

 $string = "hello world again";
 echo substr($string, 0, strripos($string, ' ')); // Hello world

1 Comment

Essentially the same code as in @Till answer, and without any explanation too.
5
$myString = "Hello World Again";
echo substr($myString, 0, strrpos($myString, " "));

Comments

1

You can use a combination of strrpos, which gets the position of the last instance of a given string within a string, and substr to return the value.

Comments

1

The correct implementation should be:

$string = "Hello World Again";
$pos = strrpos( $string, ' ');
if ($pos !== false) {
    echo substr($string, 0, $pos ); //Hello World
}

Otherwise if the character is not found it will print nothing. See following case:

$string = "Hello World Again";
//prints nothing as : is not found and strrpos returns false.
echo substr($string, 0, strrpos( $string, ':') );

Comments

1

The most direct, single-function approach is to use preg_replace() to remove the last space and all remaining characters (which logically cannot include any spaces).

This is suitable for a wide range of textual scenarios and is easy to adjust because there are just 3 basic components in the pattern.

- match a space
[^ ]* - match zero or more non-space characters
$ - match the end of the string

If there are no spaces in the input string, then the input string will remain unchanged.

Code: (Demo)

$string = "Hello World Again";
var_export(preg_replace('/ [^ ]*$/', '', $string));
// 'Hello World'

3 Comments

Underappreciated answer. Is it similar in performance to the top answer?
On a single string, no human will ever notice any difference either way. On several hundred thousands of strings, I don't know which way will be faster and I am not motivated to benchmark it to be honest. I prefer this way on the grounds of directness/brevity.
Yeah, what caught my attention was its brevity. I usually avoid regex solutions, but I like this one.
0

You could just use:

$string = "Hello World Again";
echo preg_replace('# [^ ]*$', '', $string);

This will work regardless of whether the character occurs in the string or not. It will also work if the last character is a space.

2 Comments

Dont forget the # as a delimiter!
This would actually be the approach I would use ...if the code was valid. :(
-1
function cutTo($string, $symbol) {
    return substr($string, 0, strpos($string, $symbol));
}

Comments

-2
<?php
   $str = "Hello World!";
   echo $str . "<br>";
   echo chop($str,"World!");
   // output - Hello 

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.