Sumit Rai
Sumit Rai

Reputation: 65

How to trim string from right in PHP?

I have a string example

this-is-the-example/exa

I want to trim /exa from the above line

$string1 = "this-is-the-example/exa";
$string2 = "/exa";

I am using rtrim($string1, $sting2)

But the output is this-is-the-exampl

I want to this-is-the-example as output.

Both string are dynamic and may have multiple occurrences within the string. But I only want to remove the last part. Also its not compulsory that the string2 has / in it. this may be normal string too. like a, abc too..

Upvotes: 1

Views: 3392

Answers (7)

mega6382
mega6382

Reputation: 9396

There are various approaches you can use for this:

With substr(DEMO):

function removeFromEnd($haystack, $needle)
{
    $length = strlen($needle);

    if(substr($haystack, -$length) === $needle)
    {
        $haystack = substr($haystack, 0, -$length);
    }
    return $haystack;
}


$trim = '/exa';
$str = 'this-is-the-example/exa';


var_dump(removeFromEnd($str, $trim));

With regex(DEMO):

$trim = '/exa';
$str = 'this-is-the-example/exa';

function removeFromEnd($haystack, $needle)
{
    $needle = preg_quote($needle, '/');
    $haystack = preg_replace("/$needle$/", '', $haystack);
    return $haystack;
}
var_dump(removeFromEnd($str, $trim));

Upvotes: 5

Mawg
Mawg

Reputation: 40155

To allow for error handling, if the substring is not found in the search string ...

<?php

$myString = 'this-is-the-example/exa';

//[Edit: see comment below] use strrpos, not strpos, to find the LAST occurrence 
$endPosition = strrpos($myString, '/exa');

// TodO; if endPosition === False then handle error, substring not found 

$leftPart = substr($myString, 0, $endPosition);

echo($leftPart);

?>

outputs

this-is-the-example

Upvotes: 0

Samir Selia
Samir Selia

Reputation: 7065

First explode the string, remove last element from exploded array using array_pop, then implode it back again with /.

$str = "this-is-the-example/exa";
if(strpos($str, '/') !== false)
{
    $arr = explode('/', $str);
    array_pop($arr);
    $str = implode('/', $arr);
    // output this-is-the-example
}

This will work event if you have multiple / in the URL and will remove last element only.

$str = "this-is-the-example/somevalue/exa";

if(strpos($str, '/') !== false)
{
    $arr = explode('/', $str);
    array_pop($arr);
    $str = implode('/', $arr);
    // output this-is-the-example
}

Upvotes: 2

Goma
Goma

Reputation: 1981

Say hi to strstr()

$str = 'this-is-the-example/exa';
$trim = '/exa';
$result = strstr($str, $trim, true);
echo $result;

Upvotes: 1

not_null
not_null

Reputation: 111

Hope this helps. :)

Simply try this code:

<?php
$this_example = substr("this-is-the-example/exa", 0, -4);  
echo "<br/>".$this_example; // returns "this-is-the-example"
?>

Upvotes: 0

Oulalahakabu
Oulalahakabu

Reputation: 489

the second parameter of rtrim is a character mask and not a string, your last "e" is trimed and that's normal.

COnsider using something else, regexp for example (preg_replace) to fit your needs

This keeps everything before "/" char :

$str = preg_replace('/^([^\/]*).*/','$1', 'this-is-the-example/exa');

This removes the last part.

$str = preg_replace('/^(.*)\/.*$/','$1', 'this-is-the-example/exa/mple');

Upvotes: 0

Viswanath Polaki
Viswanath Polaki

Reputation: 1402

You can use explode

<?php

$x = "this-is-the-example/exa";

$y = explode('/', $x);

echo $y[0];

Upvotes: 0

Related Questions