Pradeep Sanku
Pradeep Sanku

Reputation: 201

Return substring of a string in php

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.

Upvotes: 1

Views: 352

Answers (2)

odedta
odedta

Reputation: 2478

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

Upvotes: 5

Neha Prakash
Neha Prakash

Reputation: 591

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

Upvotes: 3

Related Questions