How to get everything after a certain character?

PhpString

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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.

Solution 3 - Php

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

Solution 4 - Php

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

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

Solution 5 - Php

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.

Solution 6 - Php

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

Solution 7 - Php

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

Solution 8 - Php

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

Solution 9 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1048676View Question on Stackoverflow
Solution 1 - PhpdatabyssView Answer on Stackoverflow
Solution 2 - Phpmike rodentView Answer on Stackoverflow
Solution 3 - PhpkenorbView Answer on Stackoverflow
Solution 4 - PhpPinonirvanaView Answer on Stackoverflow
Solution 5 - PhpnggitView Answer on Stackoverflow
Solution 6 - PhpAdyView Answer on Stackoverflow
Solution 7 - PhpAli A. DhillonView Answer on Stackoverflow
Solution 8 - PhparafatgaziView Answer on Stackoverflow
Solution 9 - PhpGerard de VisserView Answer on Stackoverflow