Reputation: 9375
$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??
Upvotes: 44
Views: 44801
Reputation: 48073
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'
Upvotes: 0
Reputation: 297
<?php
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"World!");
// output - Hello
?>
Upvotes: -2
Reputation: 4559
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
Upvotes: 6
Reputation: 66
function cutTo($string, $symbol) {
return substr($string, 0, strpos($string, $symbol));
}
Upvotes: -1
Reputation: 1
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.
Upvotes: 0
Reputation: 5301
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, ':') );
Upvotes: 1
Reputation: 23274
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));
Upvotes: 7
Reputation: 42140
$string = "Hello World Again";
echo substr($string, 0, strrpos( $string, ' ') ); //Hello World
If the character isn't found, nothing is echoed
Upvotes: 65
Reputation: 3154
$myString = "Hello World Again";
echo substr($myString, 0, strrpos($myString, " "));
Upvotes: 5
Reputation: 4607
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.
Upvotes: 1
Reputation: 42496
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);
Upvotes: 13