1

I want to remove string from a string from end.

for example I have a php string variable "SomeText1|SomeText2|SomeText3|SomeText4|SomeText5|SomeText6" and i want value "SomeText1|SomeText2|SomeText3|SomeText4|SomeText5" in PHP.

I tried using strrpos() to get the last occurrence of "|" i got index now i am stuck how to process further

Every time i want string excluding the last occurrence of "|" followed by the text.

3
  • 2
    explode to an array on the |, pop the last value off that array, implode on | again.... not rocket science.... just 3 lines of code, and no mathematics required Commented Jul 1, 2015 at 8:42
  • 1
    Use php.net/manual/en/function.substr.php Commented Jul 1, 2015 at 8:42
  • Mark Baker's solution is more out of the box than a simple substr Commented Jul 1, 2015 at 8:48

2 Answers 2

5

If you want the part after the last |:

$mystring = "SomeText1|SomeText2|SomeText3|SomeText4|SomeText5|SomeText6";
$strpos = strrpos($mystring, "|");
echo substr($mystring, $strpos);

If you want the first part before the last |:

$mystring = "SomeText1|SomeText2|SomeText3|SomeText4|SomeText5|SomeText6";
$strpos = strrpos($mystring, "|");
$strlength = strlen(substr($mystring, $strpos));
echo substr($mystring, 0, -$strlength);

Function reference:
1. http://php.net/manual/en/function.substr.php
2. http://php.net/manual/en/function.strrpos.php

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

Comments

3

You can make use of chop().. Refer this http://www.w3schools.com/php/func_string_chop.asp

1 Comment

chop() is an alias to rtrim(). According to php.net/manual/en/function.chop.php

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.