Php get string before last occurrence of character

$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??

asked May 9, 2011 at 15:56

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

If the character isn't found, nothing is echoed

answered May 9, 2011 at 16:03

meouwmeouw

41.2k10 gold badges51 silver badges69 bronze badges

3

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);

answered May 9, 2011 at 16:01

sdleihssirhcsdleihssirhc

41.3k5 gold badges52 silver badges67 bronze badges

1

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));

answered May 9, 2011 at 16:01

zafzaf

22.5k11 gold badges64 silver badges95 bronze badges

8

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

answered Jan 19, 2015 at 7:23

Php get string before last occurrence of character

R TR T

4,2093 gold badges35 silver badges47 bronze badges

1

$myString = "Hello World Again";
echo substr($myString, 0, strrpos($myString, " "));

answered May 9, 2011 at 16:03

TillTill

3,00416 silver badges18 bronze badges

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.

answered May 9, 2011 at 16:03

DaveKDaveK

4,4493 gold badges31 silver badges33 bronze badges

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, ':') );

answered Oct 3, 2012 at 9:55

Php get string before last occurrence of character

Arvind BhardwajArvind Bhardwaj

5,1935 gold badges35 silver badges48 bronze badges

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.

answered Apr 7, 2013 at 23:30

1

function cutTo($string, $symbol) {
    return substr($string, 0, strpos($string, $symbol));
}

answered Mar 5, 2014 at 14:50

";
   echo chop($str,"World!");
   // output - Hello 

?>

answered Jun 4, 2021 at 10:33

Php get string before last occurrence of character

How do you find the last occurrence of a character in a string in PHP?

The strrpos() function finds the position of the last occurrence of a string inside another string. Note: The strrpos() function is case-sensitive. Related functions: strpos() - Finds the position of the first occurrence of a string inside another string (case-sensitive)

How do I get everything before a character in PHP?

PHP: Get Substring Before Space (Or Any Other Character).
The PHP strtok() Function. The easiest way to get a substring before the first occurrence of a character such as a whitespace is to use the PHP strtok() function. ... .
The PHP strstr() Function. ... .
The PHP substr() Function. ... .
PHP explode() + list() Functions..

What is Rtrim PHP?

The rtrim() function removes whitespace or other predefined characters from the right side of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.

What is Strstr PHP?

The strstr() function searches for the first occurrence of a string inside another string. Note: This function is binary-safe. Note: This function is case-sensitive. For a case-insensitive search, use stristr() function.