user1048676
user1048676

Reputation: 10066

How to get everything after a certain character?

I've got a string and I'd like to get everything after a certain value. The string always starts off with a set of numbers and then an underscore. I'd like to get the rest of the string after the underscore. So for example if I have the following strings and what I'd like returned:

"123_String" -> "String"
"233718_This_is_a_string" -> "This_is_a_string"
"83_Another Example" -> "Another Example"

How can I go about doing something like this?

Upvotes: 211

Views: 417098

Answers (10)

databyss
databyss

Reputation: 6478

The strpos() finds the offset of the underscore, then substr grabs everything from that index plus 1, onwards.

$data = "123_String";
$whatIWant = substr($data, strpos($data, "_") + 1);
echo $whatIWant;

If you also want to check if the underscore character (_) exists in your string before trying to get it, you can use the following:

if (($pos = strpos($data, "_")) !== FALSE) {
    $whatIWant = substr($data, $pos+1);
}

Upvotes: 457

mickmackusa
mickmackusa

Reputation: 47863

I prefer to use single function calls over multiple function calls when possible. I also prefer not to create a temporary array to access a string value (so explode() and preg_match() are less attractive IMO). I would probably use preg_replace() in one of my own projects for stability, brevity, and maintainability (and because I am comfortable with regex).

Codes: (Demo)

echo explode('_', $string, 2)[1]; // the limit parameter gives reliability

echo ltrim($string, '0..9_'); // because character after first underscore is not a digit or underscore

echo preg_match('/\d+_\K.+/', $string, $m) ? $m[0] : null;

echo preg_replace('/^\d+_/', '', $string);

echo sscanf($string, '%*d_%[^\d]')[0]; // because no numbers in trailing substring

Upvotes: 0

GSite
GSite

Reputation: 954

I use strrchr(). For instance to find the extension of a file I use this function:

$string = 'filename.jpg';
$extension = strrchr( $string, '.'); //returns ".jpg"

Upvotes: 13

Gerard de Visser
Gerard de Visser

Reputation: 8040

Use this line to return the string after the symbol or return the original string if the character does not occur:

$newString = substr($string, (strrpos($string, '_') ?: -1) +1);

Upvotes: 1

arafatgazi
arafatgazi

Reputation: 429

If you want to get everything after certain characters and if those characters are located at the beginning of the string, you can use an easier solution like this:

$value = substr( '123_String', strlen( '123_' ) );
echo $value; // String

Upvotes: -1

kenorb
kenorb

Reputation: 166309

Here is the method by using explode:

$text = explode('_', '233718_This_is_a_string', 2)[1]; // Returns This_is_a_string

or:

$text = end((explode('_', '233718_This_is_a_string', 2)));

By specifying 2 for the limit parameter in explode(), it returns array with 2 maximum elements separated by the string delimiter. Returning 2nd element ([1]), will give the rest of string.


Here is another one-liner by using strpos (as suggested by @flu):

$needle = '233718_This_is_a_string';
$text = substr($needle, (strpos($needle, '_') ?: -1) + 1); // Returns This_is_a_string

Upvotes: 19

nggit
nggit

Reputation: 720

Another simple way, using strchr() or strstr():

$str = '233718_This_is_a_string';

echo ltrim(strstr($str, '_'), '_'); // This_is_a_string

In your case maybe ltrim() alone will suffice:

echo ltrim($str, '0..9_'); // This_is_a_string

But only if the right part of the string (after _) does not start with numbers, otherwise it will also be trimmed.

Upvotes: 5

mike rodent
mike rodent

Reputation: 15623

strtok is an overlooked function for this sort of thing. It is meant to be quite fast.

$s = '233718_This_is_a_string';
$firstPart = strtok( $s, '_' );
$allTheRest = strtok( '' ); 

Empty string like this will force the rest of the string to be returned.

NB if there was nothing at all after the '_' you would get a FALSE value for $allTheRest which, as stated in the documentation, must be tested with ===, to distinguish from other falsy values.

Upvotes: 37

Ali A. Dhillon
Ali A. Dhillon

Reputation: 643

$string = "233718_This_is_a_string";
$withCharacter = strstr($string, '_'); // "_This_is_a_string"
echo substr($withCharacter, 1); // "This_is_a_string"

In a single statement it would be.

echo substr(strstr("233718_This_is_a_string", '_'), 1); // "This_is_a_string"

Upvotes: 2

Ady
Ady

Reputation: 147

if anyone needs to extract the first part of the string then can try,

Query:

$s = "This_is_a_string_233718";

$text = $s."_".substr($s, 0, strrpos($s, "_"));

Output:

This_is_a_string

Upvotes: 3

Related Questions