How to get everything after a certain character?

319,831

Solution 1

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

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

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

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 5

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

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

Related videos on Youtube

user1048676
Author by

user1048676

Updated on December 25, 2021

Comments

  • user1048676
    user1048676 over 2 years

    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?

  • John Magnolia
    John Magnolia about 11 years
    What would be the best what to check if the underscore exists, e.g its optional.
  • Amal Murali
    Amal Murali about 10 years
    @JohnMagnolia: Then you can just use if (($pos = strpos($data, "_")) !== FALSE) { $whatIWant = substr($data, $pos+1); }
  • flu
    flu over 9 years
    @JohnMagnolia Or a little fancy one-liner: substr($data, (strrpos($data, '_') ?: -1) +1).
  • M H
    M H over 7 years
    @flu I think you meant to say, harder to read. IMO, less lines does not mean fancier.
  • Marcodor
    Marcodor about 7 years
    This function will return string after some substring/char or false if needle is not found: function StrAfterStr($S, $H) { return (($P = strpos($S, $H)) !== false) ? substr($S, $P + 1) : false; }
  • FreeKrishna
    FreeKrishna almost 7 years
    How would i modify this code to say fetch from a certain position 'n' upto an specific character '-'. For example: the string is "A: hello B: world | bye ;" how can i fetch the keyword 'world' (or any other sentence in that place ) knowing that it will always occur between 'B:' & '|' ? Any help is much appreciated.
  • Asaithambi Perumal
    Asaithambi Perumal almost 7 years
    This will handle your specific case (codepad.org/4mB9YhvM) The gist is that you find the position of your starting marker, then find the position of the ending marker (checking after your starting marker), then just grab the characters between that.
  • The Godfather
    The Godfather over 5 years
    If you need to get string after last occurence of the symbol, you can use strrpos
  • Quinn Comendant
    Quinn Comendant almost 5 years
    Be careful: strtok will return the portion of the string after the character if it is the first character in the string, e.g., strtok('a_b', '_') will return a but strtok('_b', '_') will return b, not an empty string as you'd expect
  • mike rodent
    mike rodent about 3 years
    Hmm re this comment, I think I wouldn't "expect" an empty string, I would probably wonder whether I was going to get an empty string...
  • Quinn Comendant
    Quinn Comendant about 3 years
    My comment was an attempt to say that this answer will break if _ is the first character. If $s in the code above is "_This_is_a_string" the result will be is_a_string, not This_is_a_string. Therefore, it is not a solution to “How to get everything after a certain character?” for every case.
  • pmiguelpinto
    pmiguelpinto almost 3 years
    $extension = substr(strrchr($string, '.'), 1); // returns "jpg"

Related